From 2f3e7ec84a6b1b7b3df39812221618ff32baf933 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Mon, 6 Nov 2023 02:54:50 +0800 Subject: Week 241 --- challenge-241/cheok-yin-fung/perl/ch-1.pl | 23 ++++++++++++++++++++ challenge-241/cheok-yin-fung/perl/ch-2.pl | 36 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 challenge-241/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-241/cheok-yin-fung/perl/ch-2.pl 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]; -- cgit