aboutsummaryrefslogtreecommitdiff
path: root/challenge-199
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-10 18:43:18 +0000
committerGitHub <noreply@github.com>2023-01-10 18:43:18 +0000
commitafcb91fc1944a3b37279d74e0bd4d81b19f899e0 (patch)
tree211d8dd83049b5157f387943494d521376a9982d /challenge-199
parentb78239e0130d4c3a0d1cc37b31fc78b2147c02da (diff)
parent7b368b1ad76737992872a200d499f7ea9732f4bf (diff)
downloadperlweeklychallenge-club-afcb91fc1944a3b37279d74e0bd4d81b19f899e0.tar.gz
perlweeklychallenge-club-afcb91fc1944a3b37279d74e0bd4d81b19f899e0.tar.bz2
perlweeklychallenge-club-afcb91fc1944a3b37279d74e0bd4d81b19f899e0.zip
Merge pull request #7398 from pjcs00/wk199
Week 199 submission
Diffstat (limited to 'challenge-199')
-rw-r--r--challenge-199/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-199/peter-campbell-smith/perl/ch-1.pl43
-rwxr-xr-xchallenge-199/peter-campbell-smith/perl/ch-2.pl57
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-199/peter-campbell-smith/blog.txt b/challenge-199/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..d9941d2f8b
--- /dev/null
+++ b/challenge-199/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2023/01/all-good-things.html
diff --git a/challenge-199/peter-campbell-smith/perl/ch-1.pl b/challenge-199/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..1f40f63a10
--- /dev/null
+++ b/challenge-199/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2023-01-09
+# PWC 199 task 1
+
+use v5.28;
+use utf8;
+use warnings;
+
+# You are given a list of integers, @list. Write a script to find the total count of Good Pairs.
+# A pair (i, j) is called good if list[i] == list[j] and i < j.
+
+# Blog: https://pjcs-pwc.blogspot.com/2023/01/all-good-things.html
+
+my (@tests, $test, @list, $i, $j, $count, $rubric);
+@tests = ([1, 2, 3, 1, 1, 3], [1, 2, 3], [1, 1, 1, 1], [1, 3, 5, 7, 9, 1, 3, 5, 7, 9, 1]);
+
+# loop over tests
+for $test (@tests) {
+
+ # initialise
+ @list = @$test;
+ $count = 0;
+ $rubric = '';
+
+ # loop over first candidate
+ for $i (0 .. scalar(@list) - 2) {
+
+ # loop over second candidate
+ for $j ($i + 1 .. scalar(@list) - 1) {
+
+ # check for goodness
+ if ($list[$i] == $list[$j]) {
+ $count ++;
+ $rubric .= qq[($i, $j) - both equal $list[$i]\n];
+ }
+ }
+ }
+
+ # report
+ say qq[Input: (] . join(', ', @list). qq[)\nOutput: $count];
+ say $rubric ? qq[Good pairs are:\n$rubric] : '';
+}
diff --git a/challenge-199/peter-campbell-smith/perl/ch-2.pl b/challenge-199/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..c3d6dbc41b
--- /dev/null
+++ b/challenge-199/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2023-01-09
+# PWC 199 task 2
+
+use v5.28;
+use utf8;
+use warnings;
+
+# You are given an array of integers, @array and three integers $x, $y, $z.
+# Write a script to find out total Good Triplets in the given array.
+# A triplet array[i], array[j], array[k] is good if it satisfies the following conditions:
+
+# 0 <= i < j < k <= n (size of given array)
+# abs(array[i] - array[j]) <= x
+# abs(array[j] - array[k]) <= y
+# abs(array[i] - array[k]) <= z
+
+# Blog: https://pjcs-pwc.blogspot.com/2023/01/all-good-things.html
+
+# loop over tests
+good_triplets(7, 2, 3, [3, 0, 1, 1, 9, 7]);
+good_triplets(0, 0, 1, [1, 1, 2, 2, 3]);
+good_triplets(1, 2, 1, [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]);
+
+sub good_triplets {
+
+ # initialise
+ my ($i, $j, $k, @array, $rubric, $count, $max_i, $max_j, $max_k);
+ my ($x, $y, $z, $a) = @_;
+ @array = @$a;
+ $max_k = scalar(@array) - 1;
+ $max_j = $max_k - 1;
+ $max_i = $max_j - 1;
+
+ # loop over i, j, k such that i < j < k
+ for $i (0 .. $max_i) {
+ for $j ($i + 1 .. $max_j) {
+
+ # if this test fails then no need to check $k
+ next unless abs($array[$i] - $array[$j]) <= $x;
+
+ # apply tests on $k
+ for $k ($j + 1 .. $max_k) {
+ if (abs($array[$j] - $array[$k]) <= $y and
+ abs($array[$i] - $array[$k]) <= $z) {
+ $rubric .= qq[($array[$i], $array[$j], $array[$k]) where (i = $i, j = $j, k = $k)\n];
+ $count ++;
+ }
+ }
+ }
+ }
+
+ # output result
+ say qq[Input: \@array = (] . join(', ', @array) . qq[), x = $x, y = $y, z = $z];
+ say $count ? qq[Output: $count\nGood triplets are:\n$rubric] : qq[Output: 0\n];
+}