aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-11-01 14:15:51 +0000
committerGitHub <noreply@github.com>2025-11-01 14:15:51 +0000
commit504cd39f25daaf12df4133ad416b0077a5e8e4bd (patch)
tree8aea3ea3d998a82a05dd5f5b879025457c709ad8
parent99ef8c328fb0fd949c2a54bffd3e047c8a724dbc (diff)
parentc8d036016456847547274836f26f9114b388b912 (diff)
downloadperlweeklychallenge-club-504cd39f25daaf12df4133ad416b0077a5e8e4bd.tar.gz
perlweeklychallenge-club-504cd39f25daaf12df4133ad416b0077a5e8e4bd.tar.bz2
perlweeklychallenge-club-504cd39f25daaf12df4133ad416b0077a5e8e4bd.zip
Merge pull request #12952 from wanderdoc/master
pwc 345 (wanderdoc)
-rw-r--r--challenge-345/wanderdoc/perl/ch-1.pl68
-rw-r--r--challenge-345/wanderdoc/perl/ch-2.pl104
2 files changed, 172 insertions, 0 deletions
diff --git a/challenge-345/wanderdoc/perl/ch-1.pl b/challenge-345/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..71c5ba0db0
--- /dev/null
+++ b/challenge-345/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,68 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given an array of integers, @ints.
+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: @ints = (1, 3, 2)
+Output: (1)
+
+
+Example 2
+
+Input: @ints = (2, 4, 6, 5, 3)
+Output: (2)
+
+
+Example 3
+
+Input: @ints = (1, 2, 3, 2, 4, 1)
+Output: (2, 4)
+
+
+Example 4
+
+Input: @ints = (5, 3, 1)
+Output: (0)
+
+
+Example 5
+
+Input: @ints = (1, 5, 1, 5, 1, 5, 1)
+Output: (1, 3, 5)
+
+=cut
+
+
+use Test2::V0 -no_srand => 1;
+
+is([peak_positions(1, 3, 2)], [1], 'Example 1');
+is([peak_positions(2, 4, 6, 5, 3)], [2], 'Example 2');
+is([peak_positions(1, 2, 3, 2, 4, 1)], [2, 4], 'Example 3');
+is([peak_positions(5, 3, 1)], [0], 'Example 4');
+is([peak_positions(1, 5, 1, 5, 1, 5, 1)], [1, 3, 5], 'Example 5');
+done_testing();
+
+sub peak_positions
+{
+ my @arr = @_;
+ if ( scalar(@arr) == 1) { return 0; }
+ my @positions;
+ for my $idx ( 0 .. $#arr )
+ {
+ if (
+ ($idx == 0 and $arr[$idx] > $arr[$idx+1]) or
+ ($idx < $#arr and $arr[$idx] > $arr[$idx-1]
+ and $arr[$idx] > $arr[$idx+1]) or
+ ($idx == $#arr and $arr[$idx] > $arr[$idx-1])
+ )
+ {
+ push @positions, $idx;
+ }
+ }
+ return @positions;
+}
diff --git a/challenge-345/wanderdoc/perl/ch-2.pl b/challenge-345/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..105b4df56e
--- /dev/null
+++ b/challenge-345/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,104 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given an integer array @ints 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 the front)
+@ans: stores the answers for each -1
+
+Rules:
+
+If $ints[i] is a positive number -> insert it at the front of @seen
+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
+
+Else -> append -1 to @ans
+
+At the end, return @ans.
+
+Example 1
+
+Input: @ints = (5, -1, -1)
+Output: (5, -1)
+
+@seen = (5)
+First -1: @ans = (5)
+Second -1: @ans = (5, -1)
+
+
+Example 2
+
+Input: @ints = (3, 7, -1, -1, -1)
+Output: (7, 3, -1)
+
+@seen = (3, 7)
+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)
+
+=cut
+
+use Test2::V0 -no_srand => 1;
+
+
+
+is([last_visitor(5, -1, -1)], [5, -1], 'Example 1');
+is([last_visitor(3, 7, -1, -1, -1)], [7, 3, -1], 'Example 2');
+is([last_visitor(2, -1, 4, -1, -1)], [2, 4, 2], 'Example 3');
+is([last_visitor(10, 20, -1, 30, -1, -1)], [20, 30, 20], 'Example 4');
+is([last_visitor(-1, -1, 5, -1)], [-1, -1, 5], 'Example 5');
+done_testing();
+
+sub last_visitor
+{
+ my @arr = @_;
+ my (@seen, @ans);
+ my $prev_int;
+ my $x = 0;
+ for my $int ( @arr )
+ {
+ if ( $int > 0 )
+ {
+ unshift @seen, $int;
+ $x = 0;
+ }
+ elsif ( $int == -1 )
+ {
+ push @ans, $x < scalar(@seen) ? $seen[$x] : $int;
+ if ( defined($prev_int) and $prev_int == -1 )
+ {
+ $x++;
+ }
+ else
+ {
+ $x = 1;
+ }
+ }
+ $prev_int = $int;
+ }
+ return @ans;
+}