aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2023-11-13 04:40:58 +0800
committerCY Fung <fungcheokyin@gmail.com>2023-11-13 04:40:58 +0800
commit8307a9efe726b46b3548530bbdb9482312204623 (patch)
treec1dfee1511b7a3f3cb29e8ba7686dc05384608d0
parent2f3e7ec84a6b1b7b3df39812221618ff32baf933 (diff)
downloadperlweeklychallenge-club-8307a9efe726b46b3548530bbdb9482312204623.tar.gz
perlweeklychallenge-club-8307a9efe726b46b3548530bbdb9482312204623.tar.bz2
perlweeklychallenge-club-8307a9efe726b46b3548530bbdb9482312204623.zip
Week 242
-rw-r--r--challenge-242/cheok-yin-fung/perl/ch-1.pl41
-rw-r--r--challenge-242/cheok-yin-fung/perl/ch-2.pl22
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-242/cheok-yin-fung/perl/ch-1.pl b/challenge-242/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..a853b92138
--- /dev/null
+++ b/challenge-242/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,41 @@
+# The Weekly Challenge 242
+# Task 1 Missing Members
+use v5.30.0;
+use warnings;
+use List::Util qw/uniqint/;
+
+sub mm {
+ my @arr1 = $_[0]->@*;
+ my @arr2 = $_[1]->@*;
+ my @brr1 = uniqint @arr1;
+ my @brr2 = uniqint @arr2;
+ my @commons;
+ for my $e1 (@brr1) {
+ for my $e2 (@brr2) {
+ push @commons, $e1 if $e1 == $e2;
+ }
+ }
+ @commons = uniqint @commons;
+ # @commons = sort {$a<=>$b} @commons;
+ # @brr1 = sort {$a<=>$b} @brr1;
+ # @brr2 = sort {$a<=>$b} @brr2;
+ return [
+ [grep {!contains([@commons], $_)} @brr1],
+ [grep {!contains([@commons], $_)} @brr2],
+ ];
+}
+
+sub contains {
+ my @arr = $_[0]->@*;
+ my $i = $_[1];
+ for (@arr) {
+ return 1 if $_ == $i;
+ }
+ return 0;
+}
+
+use Data::Printer;
+my $ex1 = mm([1,2,3], [2,4,6]);
+p $ex1;
+my $ex2 = mm([1,2,3,3], [1,1,2,2]);
+p $ex2;
diff --git a/challenge-242/cheok-yin-fung/perl/ch-2.pl b/challenge-242/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..d9a30085a4
--- /dev/null
+++ b/challenge-242/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,22 @@
+# The Weekly Challenge 242
+# Task 2 Flip Matrix
+use v5.30.0;
+use warnings;
+
+sub fm {
+ my @matrix = @_;
+ my $n = 1 + $#matrix;
+ my $n_matrix = [];
+ for my $i (0..$n-1) {
+ push $n_matrix->@*,
+ [map {0+!$_} reverse $matrix[$i]->@*];
+ }
+ return $n_matrix;
+}
+
+use Test::More tests=>2;
+use Test::Deep;
+cmp_deeply fm([1, 1, 0], [1, 0, 1], [0, 0, 0]),
+ [[1, 0, 0], [0, 1, 0], [1, 1, 1]];
+cmp_deeply fm([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]),
+ [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]];