aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-23 15:48:52 +0100
committerGitHub <noreply@github.com>2024-04-23 15:48:52 +0100
commit334fb0c779c78998ca193f2f4903887cff78c528 (patch)
tree154a8ca34a5b1259ef9f3f5ae21aa2b62910bd37
parentaed41ed119210ab32e32b82af8039f2c1457726b (diff)
parent1979b8d632c6fa51512dda5ec5eab2d614ca5022 (diff)
downloadperlweeklychallenge-club-334fb0c779c78998ca193f2f4903887cff78c528.tar.gz
perlweeklychallenge-club-334fb0c779c78998ca193f2f4903887cff78c528.tar.bz2
perlweeklychallenge-club-334fb0c779c78998ca193f2f4903887cff78c528.zip
Merge pull request #9970 from PerlBoy1967/branch-for-challenge-266
w266 - Task 1 & 2
-rwxr-xr-xchallenge-266/perlboy1967/perl/ch1.pl39
-rwxr-xr-xchallenge-266/perlboy1967/perl/ch2.pl49
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-266/perlboy1967/perl/ch1.pl b/challenge-266/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..f104fec4a6
--- /dev/null
+++ b/challenge-266/perlboy1967/perl/ch1.pl
@@ -0,0 +1,39 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 266
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-266
+
+Author: Niels 'PerlBoy' van Dijke
+
+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.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+sub uncommonWords ($line1,$line2) {
+ my %f; $f{lc $_}++ for (split /\s+/, "$line1 $line2");
+ my @w = grep { $f{$_} == 1 } sort keys %f;
+ return @w ? @w : '';
+}
+
+is([uncommonWords('Mango is sweet','Mango is sour')],['sour','sweet']);
+is([uncommonWords('Mango Mango','Orange')],['orange']);
+is([uncommonWords('Mango is Mango','Orange is Orange')],['']);
+
+done_testing;
diff --git a/challenge-266/perlboy1967/perl/ch2.pl b/challenge-266/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..0526dcd74b
--- /dev/null
+++ b/challenge-266/perlboy1967/perl/ch2.pl
@@ -0,0 +1,49 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 266
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-266
+
+Author: Niels 'PerlBoy' van Dijke
+
+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.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+sub isXmatrix ($ar) {
+ my $d = scalar @$ar-1;
+ my ($x1,$x2) = (0,$d);
+ for my $y (0 .. $d) {
+ for my $x (0 .. $d) {
+ if ($x == $x1 or $x == $x2) {
+ return 0 if $ar->[$y][$x] == 0;
+ } elsif ($ar->[$y][$x] != 0) {
+ return 0;
+ }
+ }
+ $x1++; $x2--;
+ }
+ return 1;
+}
+
+is(isXmatrix([[1,0,0,2],[0,3,4,0],
+ [0,5,6,0],[7,0,0,1]]),1,'Example 1');
+is(isXmatrix([[1,2,3],[4,5,6],[7,8,9]]),0,'Example 2');
+is(isXmatrix([[1,0,2],[0,3,0],[4,0,5]]),1,'Example 3');
+
+done_testing;