aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2023-01-01 17:24:19 +0800
committerCY Fung <fungcheokyin@gmail.com>2023-01-01 17:24:19 +0800
commit53ec8d88193dcfaccfb85559a82852924a8ccda3 (patch)
tree0f82a3bd68fcf1355ee03ccd1cb06b9f8290fc7a
parent4b1847b66c214d9b19d5020776b60414a8a44b3b (diff)
downloadperlweeklychallenge-club-53ec8d88193dcfaccfb85559a82852924a8ccda3.tar.gz
perlweeklychallenge-club-53ec8d88193dcfaccfb85559a82852924a8ccda3.tar.bz2
perlweeklychallenge-club-53ec8d88193dcfaccfb85559a82852924a8ccda3.zip
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);