aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-28 18:42:35 +0100
committerGitHub <noreply@github.com>2024-04-28 18:42:35 +0100
commit8d5352f2689e2bf4a1e98f0d73b8412590e0584d (patch)
tree3970d103bf0c1dbbe4dca1ad44badf9f0402730e
parent332ac6dc8151c3c4c9f57618a9e7aa2a220b6b68 (diff)
parent8cfc24381997e7c1cc93595385361455f5b539b1 (diff)
downloadperlweeklychallenge-club-8d5352f2689e2bf4a1e98f0d73b8412590e0584d.tar.gz
perlweeklychallenge-club-8d5352f2689e2bf4a1e98f0d73b8412590e0584d.tar.bz2
perlweeklychallenge-club-8d5352f2689e2bf4a1e98f0d73b8412590e0584d.zip
Merge pull request #9999 from boblied/w266
Week 266 from Bob Lied
-rw-r--r--challenge-266/bob-lied/README6
-rw-r--r--challenge-266/bob-lied/perl/ch-1.pl58
-rw-r--r--challenge-266/bob-lied/perl/ch-2.pl165
3 files changed, 226 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..d02460431c
--- /dev/null
+++ b/challenge-266/bob-lied/perl/ch-2.pl
@@ -0,0 +1,165 @@
+#!/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");
+
+ $matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 8], ];
+ is(xmatrix($matrix), true, "4x4 true");
+
+ $matrix->[0][1] = 9;
+ is(xmatrix($matrix), false, "4x4 q1 false");
+
+ $matrix->[0][1] = 0;
+ $matrix->[1][3] = 9;
+ is(xmatrix($matrix), false, "4x4 q2 false");
+
+ $matrix->[1][3] = 0;
+ $matrix->[0][2] = 9;
+ is(xmatrix($matrix), false, "4x4 q3 false");
+
+ $matrix->[0][2] = 0;
+ $matrix->[2][3] = 9;
+ is(xmatrix($matrix), false, "4x4 q4 false");
+
+ $matrix = [ [1, 0, 0, 0, 2],
+ [0, 3, 0, 4, 0],
+ [0, 0, 5, 0, 0],
+ [0, 6, 0, 7, 0],
+ [8, 0, 0, 0, 9], ];
+ is(xmatrix($matrix), true, "5x5 true");
+
+ $matrix->[0][1] = 9;
+ is(xmatrix($matrix), false, "5x5 q1 false");
+
+ $matrix->[0][1] = 0;
+ $matrix->[1][4] = 9;
+ is(xmatrix($matrix), false, "5x5 q2 false");
+
+ $matrix->[1][4] = 0;
+ $matrix->[3][0] = 9;
+ is(xmatrix($matrix), false, "5x5 q3 false");
+
+ $matrix->[3][0] = 0;
+ $matrix->[4][1] = 9;
+ is(xmatrix($matrix), false, "5x5 q4 false");
+
+ $matrix->[4][1] = 0;
+ $matrix->[1][2] = 9;
+ is(xmatrix($matrix), false, "5x5 vertical false");
+
+ $matrix->[1][2] = 0;
+ $matrix->[2][1] = 9;
+ is(xmatrix($matrix), false, "5x5 horizontal false");
+
+ done_testing;
+}