aboutsummaryrefslogtreecommitdiff
path: root/challenge-242
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-07 14:48:54 +0000
committerGitHub <noreply@github.com>2023-11-07 14:48:54 +0000
commitefcdd2f9c060717cbc64427dff3dc259f34c01b9 (patch)
treef06127de01dfa0d836936bd5cf69c1df2d77d4ab /challenge-242
parent6f7181a1779d2c3a77770fba875ff6c7b47a99ed (diff)
parent2173495f3a27707df6de58fa7ac0ac5f430990ee (diff)
downloadperlweeklychallenge-club-efcdd2f9c060717cbc64427dff3dc259f34c01b9.tar.gz
perlweeklychallenge-club-efcdd2f9c060717cbc64427dff3dc259f34c01b9.tar.bz2
perlweeklychallenge-club-efcdd2f9c060717cbc64427dff3dc259f34c01b9.zip
Merge pull request #9023 from choroba/ech242
Add solutions to 242: Missing Numbers & Flip Matrix by E. Choroba
Diffstat (limited to 'challenge-242')
-rwxr-xr-xchallenge-242/e-choroba/perl/ch-1.pl26
-rwxr-xr-xchallenge-242/e-choroba/perl/ch-2.pl26
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';