diff options
| author | Bob Lied <boblied+github@gmail.com> | 2024-04-24 09:32:25 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2024-04-24 09:32:25 -0500 |
| commit | 644760e7d903c5c64edaa343fae564841e83e653 (patch) | |
| tree | cbc747013f3d8f6540390a9fc85d437032510261 | |
| parent | 797322f45f4ea8e9b0c772dd3bb1436a098674a1 (diff) | |
| download | perlweeklychallenge-club-644760e7d903c5c64edaa343fae564841e83e653.tar.gz perlweeklychallenge-club-644760e7d903c5c64edaa343fae564841e83e653.tar.bz2 perlweeklychallenge-club-644760e7d903c5c64edaa343fae564841e83e653.zip | |
Week 266 solutions
| -rw-r--r-- | challenge-266/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-266/bob-lied/perl/ch-1.pl | 58 | ||||
| -rw-r--r-- | challenge-266/bob-lied/perl/ch-2.pl | 114 |
3 files changed, 175 insertions, 3 deletions
diff --git a/challenge-266/bob-lied/README b/challenge-266/bob-lied/README index 3267f8159b..e17c006f30 100644 --- a/challenge-266/bob-lied/README +++ b/challenge-266/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 264 by Bob Lied +Solutions to weekly challenge 266 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-264/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-264/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-266/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-266/bob-lied diff --git a/challenge-266/bob-lied/perl/ch-1.pl b/challenge-266/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..4b7f251356 --- /dev/null +++ b/challenge-266/bob-lied/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 266 Task 1 Uncommon Words +#============================================================================= +# 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: ('') +#============================================================================= + +use v5.38; + +use builtin qw/true false trim/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say "(", join(", ", map { "'$_'" } uncommon(@ARGV)->@* ), ")"; + + +sub uncommon(@sentenceList) +{ + use Text::ParseWords; + use List::MoreUtils qw/frequency/; + my @words = grep { defined } quotewords( '\W+', false, @sentenceList); + + my %freq = frequency(@words); + my @uncommon = grep { $freq{$_} == 1 } keys %freq; + return @uncommon ? [ sort @uncommon ] : [ '' ]; +} + +sub runTest +{ + use Test2::V0; + + is( uncommon("Mango is sweet", "Mango is sour"), [ qw(sour sweet) ], "Example 1"); + is( uncommon("Mango Mango", "Orange"), [ 'Orange' ], "Example 2"); + is( uncommon("Mango is Mango", "Orange is Orange"), [ '' ], "Example 3"); + + done_testing; +} diff --git a/challenge-266/bob-lied/perl/ch-2.pl b/challenge-266/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..8742bf1d1c --- /dev/null +++ b/challenge-266/bob-lied/perl/ch-2.pl @@ -0,0 +1,114 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 266 Task 2 X Matrix +#============================================================================= +# 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 +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +use List::Util qw/all/; +sub isZero(@elem) { return all { $_ == 0 } @elem; } +sub isNonZero(@elem) { return all { $_ != 0 } @elem; } + +# Exploit the symmetry. Given $r and $c in one quadrant, return +# the four values from all four quadrants +sub symElem($m, $end, $r, $c) +{ + return ( $m->[$r][$c], + $m->[$r][$end - $c], + $m->[$end - $r][$c], + $m->[$end - $r][$end - $c] ) +} + +sub xmatrix($matrix) +{ + my $end = $matrix->[0]->$#*; + my $mid = int($end / 2); + + my $isX = true; + for my $r ( 0 .. $mid ) + { + for my $c ( 0 .. $mid ) + { + $isX &&= ( $r == $c ) + ? isNonZero( symElem($matrix, $end, $r, $c) ) + : isZero( symElem($matrix, $end, $r, $c) ) + ; + } + } + return $isX; +} + +sub runTest +{ + use Test2::V0; +use builtin qw/true false/; no warnings "experimental::builtin"; + + my $matrix; + + $matrix = [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], ]; + is(xmatrix($matrix), true, "Example 1"); + + $matrix = [ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], ]; + is(xmatrix($matrix), false, "Example 2"); + + $matrix = [ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], ]; + is(xmatrix($matrix), true, "Example 3"); + + $matrix = [ [0, 0, 2], + [0, 3, 0], + [4, 0, 5], ]; + is(xmatrix($matrix), false, "Example 3 but false"); + + $matrix = [ [1, 2], + [3, 4], ]; + is(xmatrix($matrix), true, "2x2 true"); + + $matrix = [ [1, 0], + [3, 4], ]; + is(xmatrix($matrix), false, "2x2 false"); + + $matrix = [ [1] ]; + is(xmatrix($matrix), true, "1x1 true"); + $matrix = [ [0] ]; + is(xmatrix($matrix), false, "1x1 false"); + + done_testing; +} |
