diff options
| author | E. Choroba <choroba@matfyz.cz> | 2023-11-07 10:08:18 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2023-11-07 10:08:18 +0100 |
| commit | 2173495f3a27707df6de58fa7ac0ac5f430990ee (patch) | |
| tree | 1002c947dd8c8ca21d8d7bdd49c4ae82252677f4 | |
| parent | a82dd587d3773d2a36a1fcc4b3c525c031433a4a (diff) | |
| download | perlweeklychallenge-club-2173495f3a27707df6de58fa7ac0ac5f430990ee.tar.gz perlweeklychallenge-club-2173495f3a27707df6de58fa7ac0ac5f430990ee.tar.bz2 perlweeklychallenge-club-2173495f3a27707df6de58fa7ac0ac5f430990ee.zip | |
Add solutions to 242: Missing Numbers & Flip Matrix by E. Choroba
In Missing Numbers, I added the [] for no missing elements. The added
test case shows why I think it's needed.
| -rwxr-xr-x | challenge-242/e-choroba/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-242/e-choroba/perl/ch-2.pl | 26 |
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-242/e-choroba/perl/ch-1.pl b/challenge-242/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..93b46b9b45 --- /dev/null +++ b/challenge-242/e-choroba/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use Tie::IxHash; + +sub missing_members($arr1, $arr2) { + tie my %seen, 'Tie::IxHash'; + for my $i (0, 1) { + $seen{$_}[$i] = 1 for @{ ($arr1, $arr2)[$i] }; + } + my @missing = ([], []); + for my $e (keys %seen) { + push @{ $missing[ ! $seen{$e}[0] ] }, $e + unless 2 == grep $_, @{ $seen{$e} }; + } + return \@missing +} + +use Test2::V0; +plan 2 + 1; + +is missing_members([1, 2, 3], [2, 4, 6]), [[1, 3], [4, 6]], 'Example 1'; +is missing_members([1, 2, 3, 3], [1, 1, 2, 2]), [[3], []], 'Example 2'; +is missing_members([1, 1, 2, 2], [1, 2, 3, 3]), [[], [3]], 'First empty'; diff --git a/challenge-242/e-choroba/perl/ch-2.pl b/challenge-242/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..199eb685ce --- /dev/null +++ b/challenge-242/e-choroba/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use PDL; + +sub flip_matrix($m) { + ! pdl($m)->slice('-1:0:0') +} + +use Test::More tests => 3; + +is_deeply flip_matrix("[1 1 0]\n[0 1 1]\n[0 0 1]")->unpdl, + [[1, 0, 0], [0, 0, 1], [0, 1, 1]], + 'Unnamed example'; + +is_deeply flip_matrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]])->unpdl, + [[1, 0, 0], [0, 1, 0], [1, 1, 1]], + 'Example 1'; + +is_deeply flip_matrix( + [[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]] +)->unpdl, + [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], + 'Example 2'; |
