diff options
| -rw-r--r-- | challenge-262/cheok-yin-fung/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-262/cheok-yin-fung/perl/ch-2.pl | 21 |
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-262/cheok-yin-fung/perl/ch-1.pl b/challenge-262/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..a072bfc8d4 --- /dev/null +++ b/challenge-262/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,17 @@ +# The Weekly Challenge 262 +# Task 1 Max Positive Negative +use v5.30.0; +use warnings; + +sub mpn { + my @ints = @_; + my $p = grep {$_ > 0} @ints; + my $n = grep {$_ < 0} @ints; + return $p > $n ? $p : $n; +} + +use Test::More tests=>4; +ok mpn(-3, 1, 2, -1, 3, -2, 4) == 4; +ok mpn(-1,-2,-3, 1) == 3; +ok mpn(1,2) == 2; +ok mpn(0,0,0) == 0; diff --git a/challenge-262/cheok-yin-fung/perl/ch-2.pl b/challenge-262/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..5999690579 --- /dev/null +++ b/challenge-262/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,21 @@ +# The Weekly Challenge 262 +# Task 2 Count Equal Divisible +use v5.30.0; +use warnings; + +sub ced { + my @ints = $_[0]->@*; + my $k = $_[1]; + my $count = 0; + for my $i (0..$#ints-1) { + for my $j ($i+1..$#ints) { + $count++ if $ints[$i] == $ints[$j] && ($i*$j % $k == 0); + } + } + return $count; +} + +use Test::More tests=>3; +ok ced([3,1,2,2,2,1,3], 2) == 4; +ok ced([1,2,3], 1) == 0; +ok ced([1,2,1], 1) == 1; |
