aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-15 21:53:55 +0000
committerGitHub <noreply@github.com>2023-01-15 21:53:55 +0000
commit4269f93afde28ae12acb9bf2b2242a3132bfcdd4 (patch)
tree7feadc09246af497bc1bc7fc594dd9aca8f3124e
parentd0d2ee69829d9e7345b6ea9dc6ecf7a0201bf565 (diff)
parentfeecb8ab9a0c286c805d12532051dbb8f6e43a54 (diff)
downloadperlweeklychallenge-club-4269f93afde28ae12acb9bf2b2242a3132bfcdd4.tar.gz
perlweeklychallenge-club-4269f93afde28ae12acb9bf2b2242a3132bfcdd4.tar.bz2
perlweeklychallenge-club-4269f93afde28ae12acb9bf2b2242a3132bfcdd4.zip
Merge pull request #7419 from E7-87-83/newt
Week 199
-rw-r--r--challenge-199/cheok-yin-fung/perl/ch-1.pl20
-rw-r--r--challenge-199/cheok-yin-fung/perl/ch-2.pl27
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;