diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2023-11-06 02:54:50 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2023-11-06 02:54:50 +0800 |
| commit | 2f3e7ec84a6b1b7b3df39812221618ff32baf933 (patch) | |
| tree | 1eb48cf56d2d565d3789c75f0d3e0995f005462e | |
| parent | cef503dd4490f0574763a57cb4827e02a44b89fa (diff) | |
| download | perlweeklychallenge-club-2f3e7ec84a6b1b7b3df39812221618ff32baf933.tar.gz perlweeklychallenge-club-2f3e7ec84a6b1b7b3df39812221618ff32baf933.tar.bz2 perlweeklychallenge-club-2f3e7ec84a6b1b7b3df39812221618ff32baf933.zip | |
Week 241
| -rw-r--r-- | challenge-241/cheok-yin-fung/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-241/cheok-yin-fung/perl/ch-2.pl | 36 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-241/cheok-yin-fung/perl/ch-1.pl b/challenge-241/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..cc5c9fbbac --- /dev/null +++ b/challenge-241/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,23 @@ +# The Weekly Challenge 241 +# Task 1 Arithmetic Triplets +use v5.30.0; +use warnings; + +sub at { + my @nums = $_[0]->@*; + my $diff = $_[1]; + my $result = 0; + for my $i (0..$#nums-2) { + my $numsj = $nums[$i]+$diff; + my $numsk = $numsj+$diff; + $result++ if + (grep /^$numsj$/, @nums) + && + (grep /^$numsk$/, @nums); + } + return $result; +} + +use Test::More tests=>2; +ok at([0, 1, 4, 6, 7, 10], 3) == 2; +ok at([4, 5, 6, 7, 8, 9], 2) == 2; diff --git a/challenge-241/cheok-yin-fung/perl/ch-2.pl b/challenge-241/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..167ae917ac --- /dev/null +++ b/challenge-241/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,36 @@ +# The Weekly Challenge 241 +# Task 2 Prime Order +use v5.30.0; +use warnings; +use Math::Prime::Util qw/factor/; + +sub cp_two_same_size_arr { + my @arr0 = $_[0]->@*; + my @arr1 = $_[1]->@*; + for (0..$#arr0) { + return -1 if $arr0[$_] < $arr1[$_]; + return 1 if $arr0[$_] > $arr1[$_]; + } +} + +sub po { + my @nums = @_; + my %f; + for (@nums) { + $f{$_} = [factor $_]; + } + my @n_nums = sort { + if ($f{$a}->@* < $f{$b}->@*) { + return -1; + } elsif ($f{$a}->@* > $f{$b}->@*) { + return 1; + } else { + cp_two_same_size_arr($f{$a}, $f{$b}) + } + } @nums; + return [@n_nums]; +} + +use Test::More tests=>1; +use Test::Deep; +cmp_deeply po(11,8,27,4,12,18), [11,4,8,12,18,27]; |
