aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2023-07-24 03:57:42 +0800
committerCY Fung <fungcheokyin@gmail.com>2023-07-24 03:57:42 +0800
commitdedb05601863dcc1dd2428e6e0f6b01fb8af20a9 (patch)
treebaba14c862b4a1902fce7774a4c37c7da678d8bb
parentd80e9ac1f7ac30de8be1ebb96306ef2db5603f95 (diff)
downloadperlweeklychallenge-club-dedb05601863dcc1dd2428e6e0f6b01fb8af20a9.tar.gz
perlweeklychallenge-club-dedb05601863dcc1dd2428e6e0f6b01fb8af20a9.tar.bz2
perlweeklychallenge-club-dedb05601863dcc1dd2428e6e0f6b01fb8af20a9.zip
Week 226
-rw-r--r--challenge-226/cheok-yin-fung/perl/ch-1.pl17
-rw-r--r--challenge-226/cheok-yin-fung/perl/ch-2.pl21
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-226/cheok-yin-fung/perl/ch-1.pl b/challenge-226/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..d6de926236
--- /dev/null
+++ b/challenge-226/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,17 @@
+# The Weekly Challenge 226
+# Task 1 Shuffle String
+use v5.30.0;
+use warnings;
+
+sub ss {
+ my $string = $_[0];
+ my @indices = $_[1]->@*;
+ my @chars = split "", $string;
+ my $ans = join "", map {$chars[$_]}
+ sort {$indices[$a] <=> $indices[$b]} 0..$#chars;
+ return $ans;
+}
+
+use Test::More tests => 2;
+ok ss('lacelengh', [3,2,0,5,4,8,6,7,1]) eq 'challenge';
+ok ss('rulepark', [4,7,3,1,0,5,2,6]) eq 'perlraku';
diff --git a/challenge-226/cheok-yin-fung/perl/ch-2.pl b/challenge-226/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..1a9cb1f209
--- /dev/null
+++ b/challenge-226/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,21 @@
+# The Weekly Challenge 226
+# Task 2 Zero Array
+use v5.30.0;
+use warnings;
+use List::Util qw/all min/;
+
+sub za {
+ my @ints = $_[0]->@*;
+ my $step = 0;
+ while (!all {!$_} @ints) {
+ my $m = min grep {$_} @ints;
+ @ints = map {$_ - $m if $_} @ints;
+ $step++;
+ }
+ return $step;
+}
+
+use Test::More tests=>3;
+ok za([1, 5, 0, 3, 5]) == 3;
+ok za([0]) == 0;
+ok za([2, 1, 4, 0, 3]) == 4;