aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-20 09:35:54 +0000
committerGitHub <noreply@github.com>2022-12-20 09:35:54 +0000
commitd42580a9231abdb4e375ac49bb54e115294b46da (patch)
tree3b03aafe62d7ab224a5c9a95f41c944b80375ece
parent319c32446a61fc05eeb1a94cd4684c9f426b1244 (diff)
parent74bc3a39c66cb12964edec0aba4bbc21c12477f7 (diff)
downloadperlweeklychallenge-club-d42580a9231abdb4e375ac49bb54e115294b46da.tar.gz
perlweeklychallenge-club-d42580a9231abdb4e375ac49bb54e115294b46da.tar.bz2
perlweeklychallenge-club-d42580a9231abdb4e375ac49bb54e115294b46da.zip
Merge pull request #7286 from pjcs00/wk196
Week 196 challenges
-rw-r--r--challenge-196/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-196/peter-campbell-smith/perl/ch-1.pl54
-rwxr-xr-xchallenge-196/peter-campbell-smith/perl/ch-2.pl57
3 files changed, 112 insertions, 0 deletions
diff --git a/challenge-196/peter-campbell-smith/blog.txt b/challenge-196/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..ee550e78a6
--- /dev/null
+++ b/challenge-196/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2022/12/pattern-132-and-sequential-runs.html
diff --git a/challenge-196/peter-campbell-smith/perl/ch-1.pl b/challenge-196/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..55ac2f1585
--- /dev/null
+++ b/challenge-196/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-12-19
+# PWC 196 task 1
+
+use v5.28;
+use utf8;
+use warnings;
+
+# You are given a list of integers, @list. Write a script to find out subsequence that respects Pattern 132.
+# Pattern 132 is a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+# Return an empty array if no such sequence is found, or the first if several are found.
+
+
+# Blog: https://pjcs-pwc.blogspot.com/2022/12/pattern-132-and-sequential-runs.html
+
+my (@tests, $test, @list, $j, $last, @hard, $i, $k);
+
+# Mohammad's examples
+@tests = ([3, 1, 4, 2], [1, 2, 3, 4], [1, 3, 2, 4, 6, 5], [1, 3, 4, 2]);
+
+# a difficult one
+for $j (1 .. 10000) {
+ push @hard, $j;
+}
+push @hard, 9999;
+push @tests, \@hard;
+
+# loop over tests
+TEST: for $test (@tests) {
+ @list = @$test;
+ $last = scalar @list - 1;
+
+ # loop over j, which is the largest of the three
+ J: for $j (1 .. $last - 1) {
+
+ # find a smaller $i to the left of $j
+ for $i (0 .. $j - 1) {
+ if ($list[$i] < $list[$j]) {
+
+ # one exists so let's see if there's a smaller $k to the right of $j
+ for $k ($j + 1 .. $last) {
+ if ($list[$k] < $list[$j]) {
+ say qq[\nInput: \@list = (] . join(', ', @list) . qq[)\nOutput: ($list[$i], $list[$j], $list[$k])];
+ next TEST;
+ }
+ }
+ next J;
+ }
+ }
+ }
+ say qq[\nInput: \@list = (] . join(', ', @list) . qq[)\nOutput: none found];
+
+}
diff --git a/challenge-196/peter-campbell-smith/perl/ch-2.pl b/challenge-196/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..d9992918f7
--- /dev/null
+++ b/challenge-196/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-12-19
+# PWC 196 task 2
+
+use v5.28;
+use utf8;
+use warnings;
+
+# We are given a sorted unique integer array, @array. We are asked to find all the slices of this array
+# which comprise consecutive integers, and output the first and last element in each such slice.
+
+# Blog: https://pjcs-pwc.blogspot.com/2022/12/pattern-132-and-sequential-runs.html
+
+my (@tests, $test, @array, $start, $in_slice, $output, $j);
+
+# Mohammad's examples
+@tests = ([1,3,4,5,7], [1,2,3,6,7,9], [0,1,2,4,5,6,8,9], [1,3,5,7,9,11]);
+
+# loop over tests
+TEST: for $test (@tests) {
+ @array = @$test;
+
+ # initialise
+ $in_slice = 0;
+ $output = '';
+ say qq[\nInput: \@array = (] . join(', ', @array) . ')';
+
+ # add a number at the end of @array which is 2 more than the last one
+ push @array, $array[scalar @array - 1] + 2;
+
+ # loop over elements in @array
+ for ($j = 0; $j < (scalar @array) - 1; $j ++) {
+
+ # if we're not already in a sequence and the next element is one more than this, start a sequence
+ unless ($in_slice) {
+ if ($array[$j] == $array[$j + 1] - 1) {
+ $start = $array[$j];
+ $in_slice = 1;
+ }
+
+ # if we are already in a sequence, end it if the following element isn't one more than this
+ } else {
+ if ($array[$j] != $array[$j + 1] - 1) {
+ $output .= qq[[$start, $array[$j]], ];
+ $in_slice = 0;
+ }
+ }
+ }
+ if ($output) {
+ say qq[Output: ]. substr($output, 0, -2);
+ } else {
+ say qq[Output: no consecutive sequence found];
+ }
+}
+
+