diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-23 15:52:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-23 15:52:04 +0100 |
| commit | 4672c0618e09d918c70ef3d009eed47ebbf192fd (patch) | |
| tree | 49544593f20149187abcbfede55dd094aa071805 /challenge-266 | |
| parent | 9dab2c86dfade6f3c6820af1432462e88f7e53c4 (diff) | |
| parent | ed0eedd8722b3fb4f9e3c9ea28e6dacb047e0a20 (diff) | |
| download | perlweeklychallenge-club-4672c0618e09d918c70ef3d009eed47ebbf192fd.tar.gz perlweeklychallenge-club-4672c0618e09d918c70ef3d009eed47ebbf192fd.tar.bz2 perlweeklychallenge-club-4672c0618e09d918c70ef3d009eed47ebbf192fd.zip | |
Merge pull request #9972 from choroba/ech266
Add solutions to 266: Uncommon Words & X Matrix by E. Choroba
Diffstat (limited to 'challenge-266')
| -rwxr-xr-x | challenge-266/e-choroba/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-266/e-choroba/perl/ch-2.pl | 50 |
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'; |
