aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-28 23:36:06 +0000
committerGitHub <noreply@github.com>2025-10-28 23:36:06 +0000
commit8f54e268959eced0672a88e101d998a9b86a4945 (patch)
tree3e77fee8b673c948d6f8d7c0bb9588892d74fd5f
parentc6c0ff17754ba5eaeaee30d6686b674f4a940674 (diff)
parent78e915b67bb5001ec39a1e9aa12432545fa6905d (diff)
downloadperlweeklychallenge-club-8f54e268959eced0672a88e101d998a9b86a4945.tar.gz
perlweeklychallenge-club-8f54e268959eced0672a88e101d998a9b86a4945.tar.bz2
perlweeklychallenge-club-8f54e268959eced0672a88e101d998a9b86a4945.zip
Merge pull request #12940 from pme/challenge-345
challenge-345
-rwxr-xr-xchallenge-345/peter-meszaros/perl/ch-1.pl78
-rwxr-xr-xchallenge-345/peter-meszaros/perl/ch-2.pl102
-rwxr-xr-xchallenge-345/peter-meszaros/tcl/ch-1.tcl80
-rwxr-xr-xchallenge-345/peter-meszaros/tcl/ch-2.tcl102
4 files changed, 362 insertions, 0 deletions
diff --git a/challenge-345/peter-meszaros/perl/ch-1.pl b/challenge-345/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..8d051a87c2
--- /dev/null
+++ b/challenge-345/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Peak Positions
+
+Submitted by: Mohammad Sajid Anwar
+
+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.
+
+=head2 Example 1
+
+ Input: @ints = (1, 3, 2)
+ Output: (1)
+
+=head2 Example 2
+
+ Input: @ints = (2, 4, 6, 5, 3)
+ Output: (2)
+
+=head2 Example 3
+
+ Input: @ints = (1, 2, 3, 2, 4, 1)
+ Output: (2, 4)
+
+=head2 Example 4
+
+ Input: @ints = (5, 3, 1)
+ Output: (0)
+
+=head2 Example 5
+
+ Input: @ints = (1, 5, 1, 5, 1, 5, 1)
+ Output: (1, 3, 5)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use constant { true => 1, false => 0 };
+
+my $cases = [
+ [[1, 3, 2], [1], "Example 1"],
+ [[2, 4, 6, 5, 3], [2], "Example 2"],
+ [[1, 2, 3, 2, 4, 1], [2, 4], "Example 3"],
+ [[5, 3, 1], [0], "Example 4"],
+ [[1, 5, 1, 5, 1, 5, 1], [1, 3, 5], "Example 5"],
+];
+
+sub peak_positions
+{
+ my $ints = shift;
+
+ my @peaks;
+ my $len = @$ints;
+ for (my $i = 0; $i < $len; $i++) {
+ if ($i == 0) {
+ push @peaks, $i if $ints->[$i] > $ints->[$i + 1];
+ } elsif ($i == $len - 1) {
+ push @peaks, $i if $ints->[$i] > $ints->[$i - 1];
+ } else {
+ push @peaks, $i if $ints->[$i] > $ints->[$i - 1]
+ && $ints->[$i] > $ints->[$i + 1];
+ }
+ }
+ return \@peaks;
+}
+
+for (@$cases) {
+ is(peak_positions($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-345/peter-meszaros/perl/ch-2.pl b/challenge-345/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..e3144a498c
--- /dev/null
+++ b/challenge-345/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Last Visitor
+
+Submitted by: Mohammad Sajid Anwar
+
+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.
+
+=head2 Example 1
+
+ Input: @ints = (5, -1, -1)
+ Output: (5, -1)
+
+ @seen = (5)
+ First -1: @ans = (5)
+ Second -1: @ans = (5, -1)
+
+=head2 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)
+
+=head2 Example 3
+
+ Input: @ints = (2, -1, 4, -1, -1)
+ Output: (2, 4, 2)
+
+=head2 Example 4
+
+ Input: @ints = (10, 20, -1, 30, -1, -1)
+ Output: (20, 30, 20)
+
+=head2 Example 5
+
+ Input: @ints = (-1, -1, 5, -1)
+ Output: (-1, -1, 5)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use constant { true => 1, false => 0 };
+
+my $cases = [
+ [[ 5, -1, -1], [5, -1], "Example 1"],
+ [[ 3, 7, -1, -1, -1], [7, 3, -1], "Example 2"],
+ [[ 2, -1, 4, -1, -1], [2, 4, 2], "Example 3"],
+ [[10, 20, -1, 30, -1, -1], [20, 30, 20], "Example 4"],
+ [[-1, -1, 5, -1], [-1, -1, 5], "Example 5"],
+];
+
+sub last_visitor
+{
+ my $ints = shift;
+
+ my @seen;
+ my @ans;
+ my $minus_count = 0;
+ for my $i (@$ints) {
+ if ($i == -1) {
+ push @ans, $minus_count < @seen ? $seen[$minus_count] : -1;
+ $minus_count++;
+ } else {
+ unshift @seen, $i;
+ $minus_count = 0;
+ }
+ }
+ return \@ans;
+}
+
+for (@$cases) {
+ is(last_visitor($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-345/peter-meszaros/tcl/ch-1.tcl b/challenge-345/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..2c7b3f27e2
--- /dev/null
+++ b/challenge-345/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,80 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Peak Positions
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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)
+#
+
+package require tcltest
+
+set cases {
+ {{1 3 2} {1} "Example 1"}
+ {{2 4 6 5 3} {2} "Example 2"}
+ {{1 2 3 2 4 1} {2 4} "Example 3"}
+ {{5 3 1} {0} "Example 4"}
+ {{1 5 1 5 1 5 1} {1 3 5} "Example 5"}
+}
+
+proc peak_positions {ints} {
+ set peaks {}
+ set len [llength $ints]
+ for {set i 0} {$i < $len} {incr i} {
+ if {$i == 0} {
+ if {[lindex $ints $i] > [lindex $ints [expr $i + 1]]} {
+ lappend peaks $i
+ }
+ } elseif {$i == [expr {$len - 1}]} {
+ if {[lindex $ints $i] > [lindex $ints [expr $i - 1]]} {
+ lappend peaks $i
+ }
+ } else {
+ if {[lindex $ints $i] > [lindex $ints [expr $i - 1]] &&
+ [lindex $ints $i] > [lindex $ints [expr $i + 1]]} {
+ lappend peaks $i
+ }
+ }
+ }
+ return $peaks
+
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ peak_positions [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-345/peter-meszaros/tcl/ch-2.tcl b/challenge-345/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..4a5d998cf8
--- /dev/null
+++ b/challenge-345/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,102 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Last Visitor
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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)
+#
+
+package require tcltest
+
+set cases {
+ {{ 5 -1 -1} {5 -1} "Example 1"}
+ {{ 3 7 -1 -1 -1} {7 3 -1} "Example 2"}
+ {{ 2 -1 4 -1 -1} {2 4 2} "Example 3"}
+ {{10 20 -1 30 -1 -1} {20 30 20} "Example 4"}
+ {{-1 -1 5 -1} {-1 -1 5} "Example 5"}
+}
+
+proc last_visitor {ints} {
+ set seen {}
+ set ans {}
+ set minus_count 0
+ foreach i $ints {
+ if {$i == -1} {
+ if {$minus_count < [llength $seen]} {
+ lappend ans [lindex $seen $minus_count]
+ } else {
+ lappend ans -1
+ }
+ incr minus_count
+ } else {
+ set seen [linsert $seen 0 $i]
+ set minus_count 0
+ }
+ }
+ return $ans
+
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ last_visitor [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+