From 1368b80e4471fc70286164e81af24cb4be769f9a Mon Sep 17 00:00:00 2001 From: Matthias Muth Date: Sun, 28 Apr 2024 22:30:29 +0200 Subject: Challenge 266 Task 1 and 2 solutions in Perl by Matthias Muth --- challenge-266/matthias-muth/README.md | 9 ++-- challenge-266/matthias-muth/perl/ch-1.pl | 28 ++++++++++ challenge-266/matthias-muth/perl/ch-2.pl | 31 +++++++++++ challenge-266/matthias-muth/perl/challenge-266.txt | 62 ++++++++++++++++++++++ 4 files changed, 125 insertions(+), 5 deletions(-) create mode 100755 challenge-266/matthias-muth/perl/ch-1.pl create mode 100755 challenge-266/matthias-muth/perl/ch-2.pl create mode 100644 challenge-266/matthias-muth/perl/challenge-266.txt 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** +
+(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..a5031ee602 --- /dev/null +++ b/challenge-266/matthias-muth/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/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 %word_counts; + ++$word_counts{$_} + for "$line1 $line2" =~ /(\w+)/ig; + my @results = grep $word_counts{$_} == 1, "$line1 $line2" =~ /(\w+)/ig; + 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. -- cgit From 7ffe23d87b053bf4af6ae8724210b0060a3ebbae Mon Sep 17 00:00:00 2001 From: Matthias Muth Date: Sun, 28 Apr 2024 22:36:42 +0200 Subject: Challenge 266 Task 1 and 2 solutions in Perl by Matthias Muth --- challenge-266/matthias-muth/perl/ch-1.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/challenge-266/matthias-muth/perl/ch-1.pl b/challenge-266/matthias-muth/perl/ch-1.pl index a5031ee602..3272df450a 100755 --- a/challenge-266/matthias-muth/perl/ch-1.pl +++ b/challenge-266/matthias-muth/perl/ch-1.pl @@ -11,10 +11,11 @@ use v5.36; sub uncommon_words( $line1, $line2 ) { + my @all_words = "$line1 $line2" =~ /(\w+)/g; my %word_counts; ++$word_counts{$_} - for "$line1 $line2" =~ /(\w+)/ig; - my @results = grep $word_counts{$_} == 1, "$line1 $line2" =~ /(\w+)/ig; + for @all_words; + my @results = grep $word_counts{$_} == 1, @all_words; return @results ? @results : ""; } -- cgit