From 2173495f3a27707df6de58fa7ac0ac5f430990ee Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 7 Nov 2023 10:08:18 +0100 Subject: 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. --- challenge-242/e-choroba/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-242/e-choroba/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 challenge-242/e-choroba/perl/ch-1.pl create mode 100755 challenge-242/e-choroba/perl/ch-2.pl 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'; -- cgit