diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-15 09:42:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-15 09:42:46 +0100 |
| commit | 988fad6a7e38a53dccb0a5d76a01e9414418ff7e (patch) | |
| tree | 41389a35ee54360605679acd35b0f1315d968d91 | |
| parent | 30c224e9017864bfa1b5d81fd63a78a10f769eb6 (diff) | |
| parent | 552c97e0b3b2b4ab06c625b5277c2a5208c77aeb (diff) | |
| download | perlweeklychallenge-club-988fad6a7e38a53dccb0a5d76a01e9414418ff7e.tar.gz perlweeklychallenge-club-988fad6a7e38a53dccb0a5d76a01e9414418ff7e.tar.bz2 perlweeklychallenge-club-988fad6a7e38a53dccb0a5d76a01e9414418ff7e.zip | |
Merge pull request #8868 from E7-87-83/newt
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]; |
