diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2023-10-15 11:30:54 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2023-10-15 11:30:54 +0800 |
| commit | 552c97e0b3b2b4ab06c625b5277c2a5208c77aeb (patch) | |
| tree | 6f16db00f56e57a5d98ccf6706184df26423646f | |
| parent | d26aed3537962bc1323a7c791bee6eac82910392 (diff) | |
| download | perlweeklychallenge-club-552c97e0b3b2b4ab06c625b5277c2a5208c77aeb.tar.gz perlweeklychallenge-club-552c97e0b3b2b4ab06c625b5277c2a5208c77aeb.tar.bz2 perlweeklychallenge-club-552c97e0b3b2b4ab06c625b5277c2a5208c77aeb.zip | |
Week 238
| -rw-r--r-- | challenge-238/cheok-yin-fung/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-238/cheok-yin-fung/perl/ch-2.pl | 28 |
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-238/cheok-yin-fung/perl/ch-1.pl b/challenge-238/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..be69f62b75 --- /dev/null +++ b/challenge-238/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,16 @@ +# The Weekly Challenge 238 +# Task 1 Running Sum +use v5.30.0; +use warnings; +use List::Util qw/reductions/; + +sub rs { + my @int = @_; + return [reductions {$a+$b} @int]; +} + +use Test::More tests=>3; +use Test::Deep; +cmp_deeply rs(1, 2, 3, 4, 5), [1, 3, 6, 10, 15]; +cmp_deeply rs(1, 1, 1, 1, 1), [1, 2, 3, 4, 5]; +cmp_deeply rs(0, -1, 1, 2), [0, -1, 0, 2]; diff --git a/challenge-238/cheok-yin-fung/perl/ch-2.pl b/challenge-238/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..c92df09042 --- /dev/null +++ b/challenge-238/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,28 @@ +# The Weekly Challenge 238 +# Task 2 Persistence Sort +use v5.30.0; +use warnings; +use List::Util qw/product/; + +sub psort { + my @int = @_; + my %persist; + sub find_persist { + my $persist = 0; + my $res = $_[0]; + while ($res >= 10) { + $res = product split "", $res; + $persist++; + } + return $persist; + } + for my $i (@int) { + $persist{$i} = find_persist($i); + } + return [sort { $persist{$a} <=> $persist{$b} || $a <=> $b } @int] +} + +use Test::More tests=>2; +use Test::Deep; +cmp_deeply psort(15, 99, 1, 34), [1, 15, 34, 99]; +cmp_deeply psort(50, 25, 33, 22), [22, 33, 50, 25]; |
