diff options
| -rw-r--r-- | challenge-199/cheok-yin-fung/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-199/cheok-yin-fung/perl/ch-2.pl | 27 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-199/cheok-yin-fung/perl/ch-1.pl b/challenge-199/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..f403725e3c --- /dev/null +++ b/challenge-199/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,20 @@ +# The Weekly Challenge 199 +# Task 1 Good Pairs +use v5.30.0; +use warnings; + +sub good_pairs { + my @a = $_[0]->@*; + my $ans = 0; + for my $i (0..$#a) { + for my $j ($i+1..$#a) { + $ans++ if $a[$i] == $a[$j]; + } + } + return $ans; +} + +use Test::More tests=>3; +ok good_pairs([1,2,3,1,1,3]) == 4; +ok good_pairs([1,2,3]) == 0; +ok good_pairs([1,1,1,1]) == 6; diff --git a/challenge-199/cheok-yin-fung/perl/ch-2.pl b/challenge-199/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..c6986adaa2 --- /dev/null +++ b/challenge-199/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,27 @@ +# The Weekly Challenge 199 +# Task 2 Good Triplets +use v5.30.0; +use warnings; + +sub good_triplets { + my @a = $_[0]->@*; + my ($x, $y, $z) = ($_[1], $_[2], $_[3]); + my $ans = 0; + for my $i (0..$#a) { + for my $j ($i+1..$#a) { + for my $k ($j+1..$#a) { + $ans++ if + (abs($a[$i]-$a[$j]) <= $x) + && + (abs($a[$j]-$a[$k]) <= $y) + && + (abs($a[$k]-$a[$i]) <= $z); + } + } + } + return $ans; +} + +use Test::More tests=>2; +ok good_triplets([3,0,1,1,9,7], 7, 2, 3) == 4; +ok good_triplets([1,1,2,2,3], 0, 0, 1) == 0; |
