aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-19 20:16:51 +0000
committerGitHub <noreply@github.com>2023-03-19 20:16:51 +0000
commita5a72532d7a1fa425e667af7dff9f77f29f07a4d (patch)
treef7d1b1c1fdd28f29839f7483344ce34f28ffb4d9
parent4788d0fe110c7801a9b5fb0fc769e288f9b00a8b (diff)
parent222c407ed9145671bad29482650041e5f45dba4a (diff)
downloadperlweeklychallenge-club-a5a72532d7a1fa425e667af7dff9f77f29f07a4d.tar.gz
perlweeklychallenge-club-a5a72532d7a1fa425e667af7dff9f77f29f07a4d.tar.bz2
perlweeklychallenge-club-a5a72532d7a1fa425e667af7dff9f77f29f07a4d.zip
Merge pull request #7757 from E7-87-83/newt
Week 208
-rw-r--r--challenge-208/cheok-yin-fung/perl/ch-1.pl43
-rw-r--r--challenge-208/cheok-yin-fung/perl/ch-2.pl40
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-208/cheok-yin-fung/perl/ch-1.pl b/challenge-208/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..1cddf391f5
--- /dev/null
+++ b/challenge-208/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,43 @@
+# The Weekly Challenge 208
+# Task 1 Minimum Index Sum
+use v5.30.0;
+use warnings;
+use Array::Utils qw/intersect/;
+
+sub aindex {
+ my $item = $_[0];
+ my @list = $_[1]->@*;
+ for my $i (0..$#list) {
+ return $i if $item eq $list[$i];
+ }
+ return undef;
+}
+
+sub mis {
+ my @ans = ();
+ my @list1 = $_[0]->@*;
+ my @list2 = $_[1]->@*;
+ my @comm = intersect(@list1, @list2);
+ my $mis = $#list1 + $#list2;
+ foreach (@comm) {
+ my $ind_sum = aindex($_, [@list1]) + aindex($_, [@list2]);
+ if ($ind_sum == $mis) {
+ push @ans, $_;
+ } elsif ($ind_sum < $mis) {
+ $mis = $ind_sum;
+ @ans = ($_);
+ }
+ }
+ return [sort {$a cmp $b} @ans];
+}
+
+use Test::More tests=>3;
+use Test::Deep;
+cmp_deeply
+ mis(["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"]),
+ ["Perl", "Raku"];
+cmp_deeply mis(["A", "B", "C"], ["D", "E", "F"]),
+ [];
+cmp_deeply
+ mis(["A", "B", "C"], ["C", "A", "B"]),
+ ["A"];
diff --git a/challenge-208/cheok-yin-fung/perl/ch-2.pl b/challenge-208/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..2d009f2511
--- /dev/null
+++ b/challenge-208/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,40 @@
+# The Weekly Challenge 208
+# Task 2 Duplicate and Missing
+# Assume the array has three or more elements.
+use v5.30.0;
+use warnings;
+use List::MoreUtils qw/slide/;
+
+sub aindex {
+ my $item = $_[0];
+ my @list = $_[1]->@*;
+ for my $i (0..$#list) {
+ return $i if $item == $list[$i];
+ }
+ return undef;
+}
+
+sub dm {
+ my @arr = @_;
+ my $dup, my $mis;
+ my $locator;
+ $locator = $arr[0]!=$arr[1] ? $arr[0] : $arr[-1];
+ my $ref_ind = $locator==$arr[0] ? 0 : $#arr;
+ my $shft = $locator-$ref_ind;
+ for my $i (0..$#arr) {
+ if ($shft+$i != $arr[$i]) {
+ ($dup, $mis) = ($arr[$i], $shft+$i);
+ last;
+ }
+ }
+ return [$dup, $mis] if defined($dup);
+ return -1;
+}
+
+use Test::More tests=>5;
+use Test::Deep;
+cmp_deeply dm(1,2,2,4), [2,3];
+cmp_deeply dm(1,2,3,4), -1;
+cmp_deeply dm(1,2,3,3), [3,4];
+cmp_deeply dm(2,2,3), [2,1];
+cmp_deeply dm(3,3,4), [3,2];