aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-27 14:31:19 +0000
committerGitHub <noreply@github.com>2025-10-27 14:31:19 +0000
commitdd0b42d3cf5d83c2b6d38485422b5778a32f1e6a (patch)
tree6537d67d56243250300cf18c16e3f7c100a34bb4
parent9e0b45f5079ce689c62500d70642709a80bee0a5 (diff)
parent7c0bca87cde4d66f357f42a9388d82166d8f9568 (diff)
downloadperlweeklychallenge-club-dd0b42d3cf5d83c2b6d38485422b5778a32f1e6a.tar.gz
perlweeklychallenge-club-dd0b42d3cf5d83c2b6d38485422b5778a32f1e6a.tar.bz2
perlweeklychallenge-club-dd0b42d3cf5d83c2b6d38485422b5778a32f1e6a.zip
Merge pull request #12931 from pjcs00/wk345
Week 345 - Peak at the answers
-rw-r--r--challenge-345/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-345/peter-campbell-smith/perl/ch-1.pl36
-rwxr-xr-xchallenge-345/peter-campbell-smith/perl/ch-2.pl52
3 files changed, 89 insertions, 0 deletions
diff --git a/challenge-345/peter-campbell-smith/blog.txt b/challenge-345/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..1800695822
--- /dev/null
+++ b/challenge-345/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/345
diff --git a/challenge-345/peter-campbell-smith/perl/ch-1.pl b/challenge-345/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..01f705f5cb
--- /dev/null
+++ b/challenge-345/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-10-27
+use utf8; # Week 345 - task 1 - Peak positions
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+peak_positions(1, 2, 3, 2, 1);
+peak_positions(1, 2, 3, 4, 5);
+peak_positions(3, 3, 3, 3, 3);
+peak_positions(3, 2, 1, 2, 3);
+peak_positions(1, 2, 1, 3, 1, 4, 1, 6, 1);
+peak_positions(1, 2);
+peak_positions(-3, -2, -4, 0, 9, -100);
+
+sub peak_positions {
+
+ my (@ints, @peaks, $j);
+
+ # initialise
+ @ints = @_;
+
+ # do as instructed
+ if ($#ints > 2) {
+ for $j (1 .. $#ints - 1) {
+ push @peaks, $j if ($ints[$j] > $ints[$j - 1]
+ and $ints[$j] > $ints[$j + 1]);
+ }
+ }
+
+ say qq[\nInput: (] . join(', ', @ints) . ')';
+ say qq[Output: (] . join(', ', @peaks) . ')';
+}
diff --git a/challenge-345/peter-campbell-smith/perl/ch-2.pl b/challenge-345/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..2f822e28b7
--- /dev/null
+++ b/challenge-345/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-10-27
+use utf8; # Week 345 - task 2 - Last visitor
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+last_visitor(5, -1, -1);
+last_visitor(3, 7, -1, -1, -1);
+last_visitor(2, -1, 4, -1, -1);
+last_visitor(10, 20, -1, 30, -1, -1);
+last_visitor(-1, -1, 5, -1);
+
+sub last_visitor {
+
+ my (@ints, @ans, @seen, $i, $x);
+
+ # initialise
+ @ints = @_;
+ $x = 0;
+
+ for $i (0 .. $#ints) {
+
+ # if $ints[i] is +ve push it onto @seen
+ if ($ints[$i] > 0) {
+ push @seen, $ints[$i];
+ $x = 0;
+
+ # if $ints[$i] is -1 ...
+ } elsif ($ints[$i] == -1) {
+
+ # ... and $x <= length of @seen
+ if ($x <= $#seen) {
+
+ # push $x'th from last element of @seen onto @ans
+ push @ans, $seen[$#seen - $x];
+ } else {
+
+ # else push -1 onto @ans
+ push @ans, -1;
+ }
+
+ # increment no of consecutive -1s
+ $x ++;
+ }
+ }
+ say qq[\nInput: (] . join(', ', @ints) . ')';
+ say qq[Output: (] . join(', ', @ans) . ')';
+}