aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-29 20:35:08 +0000
committerGitHub <noreply@github.com>2025-10-29 20:35:08 +0000
commita0eb6d9836cd3edf31f61d370a62d2582a86605b (patch)
tree0e771ccab03ec42f1f6a99d5821109cb3bc1cab5
parent68797ec2469fa497291f5eb7fccb3b24eece9bff (diff)
parent6d1c1e64fb0ea51c2bbca3dacb0b84e402d90d4a (diff)
downloadperlweeklychallenge-club-a0eb6d9836cd3edf31f61d370a62d2582a86605b.tar.gz
perlweeklychallenge-club-a0eb6d9836cd3edf31f61d370a62d2582a86605b.tar.bz2
perlweeklychallenge-club-a0eb6d9836cd3edf31f61d370a62d2582a86605b.zip
Merge pull request #12942 from robbie-hatley/rh345
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #345.
-rw-r--r--challenge-345/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-345/robbie-hatley/perl/ch-1.pl76
-rwxr-xr-xchallenge-345/robbie-hatley/perl/ch-2.pl116
3 files changed, 193 insertions, 0 deletions
diff --git a/challenge-345/robbie-hatley/blog.txt b/challenge-345/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..21b742b653
--- /dev/null
+++ b/challenge-345/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2025/10/robbie-hatleys-solutions-in-perl-for_29.html
diff --git a/challenge-345/robbie-hatley/perl/ch-1.pl b/challenge-345/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..cdbc7a1962
--- /dev/null
+++ b/challenge-345/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 345-1,
+written by Robbie Hatley on Mon Oct 27, 2025.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 345-1: Peak Positions
+Submitted by: Mohammad Sajid Anwar
+You are given an array of real numbers. Find all the peaks in the
+array. (A "peak" is an element that is strictly greater than its
+left and right neighbours.) Return the indices of all such peak
+positions.
+
+Example #1: Input: (1, 3, 2) Output: (1)
+Example #2: Input: (2, 4, 6, 5, 3) Output: (2)
+Example #3: Input: (1, 2, 3, 2, 4, 1) Output: (2, 4)
+Example #4: Input: (5, 3, 1) Output: ()
+Example #5: Input: (1, 5, 1, 5, 1, 5, 1) Output: (1, 3, 5)
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+To solve this problem, I riffle through the middle indices using a ranged for loop, collect all "peak" indices
+found into an array, and return that array.
+
+--------------------------------------------------------------------------------------------------------------
+IO NOTES:
+Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
+single-quoted array of arrays of real numbers, in proper Perl syntax, like so:
+
+./ch-1.pl '([1,17,2,36,3,84,4,22,14],[1.34,2.86,9.83,17.42,16.45])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.36;
+ use utf8::all;
+
+ # What are the peak positions
+ # of an array of numbers?
+ sub peak_positions ($aref) {
+ my @pp;
+ for (1..$#$aref-1) {
+ if ($$aref[$_]>$$aref[$_-1]
+ &&$$aref[$_]>$$aref[$_+1]) {
+ push @pp, $_}}
+ @pp}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ [1, 3, 2] , # Expected output: (1)
+ [2, 4, 6, 5, 3] , # Expected output: (2)
+ [1, 2, 3, 2, 4, 1] , # Expected output: (2, 4)
+ [5, 3, 1] , # Expected output: ()
+ [1, 5, 1, 5, 1, 5, 1] , # Expected output: (1, 3, 5)
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ say '';
+ say "Array = (@$aref)";
+ my @pp = peak_positions($aref);
+ say "Peaks = (@pp)";
+}
diff --git a/challenge-345/robbie-hatley/perl/ch-2.pl b/challenge-345/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..1990fa2491
--- /dev/null
+++ b/challenge-345/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,116 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 345-2,
+written by Robbie Hatley on Dow Mon Dm, 2025.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 345-2: Last Visitor
+Submitted by: Mohammad Sajid Anwar
+You are given an array @ints of integers where each element is
+either a positive integer or -1. We process the array from left
+to right while maintaining two lists:
+@seen (stores previously-seen positive integers, newest at front)
+@ans (stores the answers for each -1)
+
+Rules:
+
+1. Iterate index $i from 0 to $#ints, strictly increasing.
+
+2. If $ints[$i] is a positive integer, then insert it at the
+ front of @seen.
+
+3. If $ints[$i] is -1, let $x be how many -1s in a row we’ve seen
+ before this one. If $x < len(@seen) -> append seen[$x] to @ans;
+ otherwise, append -1 to @ans.
+
+4. At the end, return @ans.
+
+Example #1: Input: (5, -1, -1)
+ Output: (5, -1)
+ @seen = (5)
+ First -1: @ans = (5)
+ Second -1: @ans = (5, -1)
+
+Example #2: Input: (3, 7, -1, -1, -1)
+ Output: (7, 3, -1)
+ @seen = (7, 3)
+ First -1: @ans = (7)
+ Second -1: @ans = (7, 3)
+ Third -1: @ans = (7, 3, -1)
+
+Example #3: Input: @ints = (2, -1, 4, -1, -1)
+ Output: (2, 4, 2)
+
+Example #4: Input: @ints = (10, 20, -1, 30, -1, -1)
+ Output: (20, 30, 20)
+
+Example #5: Input: @ints = (-1, -1, 5, -1)
+ Output: (-1, -1, 5)
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+This is just a matter of doing exactly what the description says. The logic is straightforward (if a bit
+perplexing) with no ambiguities, race conditions, or other entanglements, so this should be easy to program.
+
+--------------------------------------------------------------------------------------------------------------
+IO NOTES:
+Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
+single-quoted array of arrays of items which are or aren't -1, in proper Perl syntax, like so:
+
+./ch-2.pl '([5,17,-1,42,-1,86,-1,-1,-1,-1],[8,"dog",-1,36,-1,-1,-1,-1])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.36;
+ use utf8::all;
+
+ sub last_visitor ($aref) {
+ my @ints = @$aref;
+ my @seen;
+ my @ans;
+ my $x = -1;
+ for my $i (0..$#ints) {
+ if ('-1' eq $ints[$i]) {
+ ++$x;
+ if ($x < scalar(@seen)) {
+ push @ans, $seen[$x]}
+ else {
+ push @ans, -1}}
+ else {
+ $x = -1;
+ unshift @seen, $ints[$i];
+ }
+ }
+ @ans}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Input: Expected output:
+ [5, -1, -1], # (5, -1)
+ [3, 7, -1, -1, -1], # (7, 3, -1)
+ [2, -1, 4, -1, -1], # (2, 4, 2)
+ [10, 20, -1, 30, -1, -1], # (20, 30, 20)
+ [-1, -1, 5, -1], # (-1, -1, 5)
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ say '';
+ say "Array = (@$aref)";
+ my @lv = last_visitor($aref);
+ say "Last Visitors: (@lv)";
+}