aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2024-04-22 10:52:33 +0200
committerE. Choroba <choroba@matfyz.cz>2024-04-22 10:52:33 +0200
commited0eedd8722b3fb4f9e3c9ea28e6dacb047e0a20 (patch)
tree9d011fdb9dc164dc58a8c0a8dae8bb9a4bf210a7
parentaed41ed119210ab32e32b82af8039f2c1457726b (diff)
downloadperlweeklychallenge-club-ed0eedd8722b3fb4f9e3c9ea28e6dacb047e0a20.tar.gz
perlweeklychallenge-club-ed0eedd8722b3fb4f9e3c9ea28e6dacb047e0a20.tar.bz2
perlweeklychallenge-club-ed0eedd8722b3fb4f9e3c9ea28e6dacb047e0a20.zip
Add solutions to 266: Uncommon Words & X Matrix by E. Choroba
-rwxr-xr-xchallenge-266/e-choroba/perl/ch-1.pl30
-rwxr-xr-xchallenge-266/e-choroba/perl/ch-2.pl50
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-266/e-choroba/perl/ch-1.pl b/challenge-266/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..c150f01f1b
--- /dev/null
+++ b/challenge-266/e-choroba/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub uncommon_words($line1, $line2) {
+ my %seen;
+ ++$seen{$1}{1} while $line1 =~ /(\w+)/g;
+ ++$seen{$1}{2} while $line2 =~ /(\w+)/g;
+ my @uncommon = grep {
+ ($seen{$_}{1} // 0) + ($seen{$_}{2} // 0) == 1
+ } keys %seen;
+ return [""] unless @uncommon;
+ return \@uncommon
+}
+
+use Test2::V0;
+plan(3);
+
+is uncommon_words('Mango is sweet', 'Mango is sour'),
+ bag { item $_ for 'sweet', 'sour'; end() },
+ 'Example 1';
+
+is uncommon_words('Mango Mango', 'Orange'),
+ ['Orange'],
+ 'Example 2';
+
+is uncommon_words('Mango is Mango', 'Orange is Orange'),
+ [""],
+ 'Example 3';
diff --git a/challenge-266/e-choroba/perl/ch-2.pl b/challenge-266/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..af99cc16cb
--- /dev/null
+++ b/challenge-266/e-choroba/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use PDL;
+
+sub x_matrix($matrix) {
+ my $m = pdl($matrix);
+ my $diagonal = $m->diagonal(0, 1);
+ return unless $diagonal->prod;
+
+ my $antidiagonal = rotate_matrix($m)->diagonal(0, 1);
+ return unless $diagonal->prod;
+
+ $diagonal .= 0;
+ $antidiagonal .= 0;
+ return if any($m);
+
+ return 1
+}
+
+sub rotate_matrix($m) {
+ $m->slice([], [-1, 0])
+}
+
+use Test::More tests => 3 + 1;
+
+ok x_matrix([ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+]), 'Example 1';
+
+
+ok ! x_matrix([ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+]), 'Example 2';
+
+ok x_matrix([ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]), 'Example 3';
+
+ok ! x_matrix([ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 0, 0],
+ [7, 0, 0, 1],
+]), 'Zero on a diagonal';