aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-01 12:08:10 +0000
committerGitHub <noreply@github.com>2023-01-01 12:08:10 +0000
commit78f4bda807c6bfe0777a3b13bb1ae2bc9136f6ea (patch)
tree332559526c66ced3a7b9d6200d0b27405c56b393
parent0ff44b94d710ec989ae3a2ad0808c9e9cadb7c81 (diff)
parent53ec8d88193dcfaccfb85559a82852924a8ccda3 (diff)
downloadperlweeklychallenge-club-78f4bda807c6bfe0777a3b13bb1ae2bc9136f6ea.tar.gz
perlweeklychallenge-club-78f4bda807c6bfe0777a3b13bb1ae2bc9136f6ea.tar.bz2
perlweeklychallenge-club-78f4bda807c6bfe0777a3b13bb1ae2bc9136f6ea.zip
Merge pull request #7332 from E7-87-83/newt
week 197
-rw-r--r--challenge-197/cheok-yin-fung/perl/ch-1.pl25
-rw-r--r--challenge-197/cheok-yin-fung/perl/ch-2.pl18
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-197/cheok-yin-fung/perl/ch-1.pl b/challenge-197/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..3c7adf67f0
--- /dev/null
+++ b/challenge-197/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,25 @@
+# The Weekly Challenge 197
+# Task 1 Move Zero
+use v5.30.0;
+use warnings;
+
+sub mz {
+ my @a = @_;
+ for my $i (0..$#a-1) {
+ my $j = $i;
+ while ($j >= 0 && $a[$j+1] != 0 && $a[$j] == 0) {
+ ($a[$j], $a[$j+1]) = ($a[$j+1], $a[$j]);
+ $j--;
+ }
+ }
+ return @a;
+}
+
+
+use Test::More tests=>3;
+use Test::Deep;
+
+cmp_deeply [mz(1, 0, 3, 0, 0, 5)], [1, 3, 5, 0, 0, 0];
+cmp_deeply [mz(1, 6, 4)], [1, 6, 4];
+cmp_deeply [mz(0, 1, 0, 2, 0)], [1, 2, 0, 0, 0];
+
diff --git a/challenge-197/cheok-yin-fung/perl/ch-2.pl b/challenge-197/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..1499764d12
--- /dev/null
+++ b/challenge-197/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,18 @@
+# The Weekly Challenge 197
+# Task 2 Wiggle Sort
+use v5.30.0;
+use warnings;
+
+sub ws {
+ my @a = @_;
+ for my $i (0..$#a-1) {
+ ($a[$i], $a[$i+1]) = ($a[$i+1], $a[$i])
+ if ($a[$i] < $a[$i+1] && $i % 2 == 1)
+ ||
+ ($a[$i] > $a[$i+1] && $i % 2 == 0);
+ }
+ return @a;
+}
+
+say join ",", ws(1,5,1,1,6,4);
+say join ",", ws(1,3,2,2,3,1);