From 552c97e0b3b2b4ab06c625b5277c2a5208c77aeb Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 15 Oct 2023 11:30:54 +0800 Subject: Week 238 --- challenge-238/cheok-yin-fung/perl/ch-1.pl | 16 ++++++++++++++++ challenge-238/cheok-yin-fung/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 challenge-238/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-238/cheok-yin-fung/perl/ch-2.pl 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]; -- cgit