aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-28 21:42:02 +0100
committerGitHub <noreply@github.com>2024-04-28 21:42:02 +0100
commita16bc97a537ec332b46a7f84591011141207f17d (patch)
tree70eb019a681a77f598b8b6ca1652f86e28c49b55
parent683ced9865489122141ff09bc3c1bff56e8117e9 (diff)
parent7ffe23d87b053bf4af6ae8724210b0060a3ebbae (diff)
downloadperlweeklychallenge-club-a16bc97a537ec332b46a7f84591011141207f17d.tar.gz
perlweeklychallenge-club-a16bc97a537ec332b46a7f84591011141207f17d.tar.bz2
perlweeklychallenge-club-a16bc97a537ec332b46a7f84591011141207f17d.zip
Merge pull request #10002 from MatthiasMuth/muthm-266
Challenge 266 Task 1 and 2 solutions in Perl by Matthias Muth
-rw-r--r--challenge-266/matthias-muth/README.md9
-rwxr-xr-xchallenge-266/matthias-muth/perl/ch-1.pl29
-rwxr-xr-xchallenge-266/matthias-muth/perl/ch-2.pl31
-rw-r--r--challenge-266/matthias-muth/perl/challenge-266.txt62
4 files changed, 126 insertions, 5 deletions
diff --git a/challenge-266/matthias-muth/README.md b/challenge-266/matthias-muth/README.md
index e5cbc4eea2..2a22c217dc 100644
--- a/challenge-266/matthias-muth/README.md
+++ b/challenge-266/matthias-muth/README.md
@@ -1,6 +1,5 @@
-## The Weekly Challenge
-## Solutions in Perl by Matthias Muth
+**Challenge 266 solutions in Perl by Matthias Muth**
+<br/>
+(sorry, no blog post this time...)
-See [here](perl/#readme) for a blog post describing this week's solutions.
-
-#### Thank you for the challenge!
+**Thank you for the challenge!**
diff --git a/challenge-266/matthias-muth/perl/ch-1.pl b/challenge-266/matthias-muth/perl/ch-1.pl
new file mode 100755
index 0000000000..3272df450a
--- /dev/null
+++ b/challenge-266/matthias-muth/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+#
+# The Weekly Challenge - Perl & Raku
+# (https://theweeklychallenge.org)
+#
+# Challenge 266 Task 1: Uncommon Words
+#
+# Perl solution by Matthias Muth.
+#
+
+use v5.36;
+
+sub uncommon_words( $line1, $line2 ) {
+ my @all_words = "$line1 $line2" =~ /(\w+)/g;
+ my %word_counts;
+ ++$word_counts{$_}
+ for @all_words;
+ my @results = grep $word_counts{$_} == 1, @all_words;
+ return @results ? @results : "";
+}
+
+use Test2::V0 qw( -no_srand );
+is [ uncommon_words( "Mango is sweet", "Mango is sour" ) ], [ "sweet", "sour" ],
+ 'Example 1: uncommon_words( "Mango is sweet", "Mango is sour" ) == ("sweet", "sour")';
+is [ uncommon_words( "Mango Mango", "Orange" ) ], [ "Orange" ],
+ 'Example 2: uncommon_words( "Mango Mango", "Orange" ) == "Orange"';
+is [ uncommon_words( "Mango is Mango", "Orange is Orange" ) ], [ "" ],
+ 'Example 3: uncommon_words( "Mango is Mango", "Orange is Orange" ) == ""';
+done_testing;
diff --git a/challenge-266/matthias-muth/perl/ch-2.pl b/challenge-266/matthias-muth/perl/ch-2.pl
new file mode 100755
index 0000000000..9ed2be90d8
--- /dev/null
+++ b/challenge-266/matthias-muth/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+#
+# The Weekly Challenge - Perl & Raku
+# (https://theweeklychallenge.org)
+#
+# Challenge 266 Task 2: X Matrix
+#
+# Perl solution by Matthias Muth.
+#
+
+use v5.36;
+
+sub x_matrix( $matrix ) {
+ for my $i ( 0..$matrix->$#* ) {
+ for my $j ( 0..$matrix->$#* ) {
+ return 0
+ if ( ( $i == $j || $i == $matrix->$#* - $j )
+ xor $matrix->[$i][$j] != 0 );
+ }
+ }
+ return 1;
+}
+
+use Test2::V0 qw( -no_srand );
+ok x_matrix( [[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]] ),
+ 'Example 1: x_matrix( [[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]] ) is true';
+ok ! x_matrix( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ),
+ 'Example 2: x_matrix( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) is false';
+ok x_matrix( [[1, 0, 2], [0, 3, 0], [4, 0, 5]] ),
+ 'Example 3: x_matrix( [[1, 0, 2], [0, 3, 0], [4, 0, 5]] ) is true';
+done_testing;
diff --git a/challenge-266/matthias-muth/perl/challenge-266.txt b/challenge-266/matthias-muth/perl/challenge-266.txt
new file mode 100644
index 0000000000..b2c4b9c845
--- /dev/null
+++ b/challenge-266/matthias-muth/perl/challenge-266.txt
@@ -0,0 +1,62 @@
+The Weekly Challenge - 266
+Monday, Apr 22, 2024
+
+
+Task 1: Uncommon Words
+Submitted by: Mohammad Sajid Anwar
+
+You are given two sentences, $line1 and $line2.
+Write a script to find all uncommmon words in any order in the given two sentences. Return ('') if none found.
+A word is uncommon if it appears exactly once in one of the sentences and doesn’t appear in other sentence.
+Example 1
+
+Input: $line1 = 'Mango is sweet'
+ $line2 = 'Mango is sour'
+Output: ('sweet', 'sour')
+
+Example 2
+
+Input: $line1 = 'Mango Mango'
+ $line2 = 'Orange'
+Output: ('Orange')
+
+Example 3
+
+Input: $line1 = 'Mango is Mango'
+ $line2 = 'Orange is Orange'
+Output: ('')
+
+
+Task 2: X Matrix
+Submitted by: Mohammad Sajid Anwar
+
+You are given a square matrix, $matrix.
+Write a script to find if the given matrix is X Matrix.
+A square matrix is an X Matrix if all the elements on the main diagonal and antidiagonal are non-zero and everything else are zero.
+Example 1
+
+Input: $matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ]
+Output: true
+
+Example 2
+
+Input: $matrix = [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]
+Output: false
+
+Example 3
+
+Input: $matrix = [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]
+Output: true
+
+
+Last date to submit the solution 23:59 (UK Time) Sunday 28th April 2024.