aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvinodk89 <vinodkk89@gmail.com>2025-11-01 18:15:48 +0530
committervinodk89 <vinodkk89@gmail.com>2025-11-01 18:15:48 +0530
commit381782de74b3cafbcfc6faad89db7712bbba7f18 (patch)
tree0d581b07a1ffc0535f8f975c54502c23d3547766
parent88a930c6a8de82b2c59f19958b4e1493ede4a4e0 (diff)
downloadperlweeklychallenge-club-381782de74b3cafbcfc6faad89db7712bbba7f18.tar.gz
perlweeklychallenge-club-381782de74b3cafbcfc6faad89db7712bbba7f18.tar.bz2
perlweeklychallenge-club-381782de74b3cafbcfc6faad89db7712bbba7f18.zip
Solutions for Challenge-345 (Perl)
-rw-r--r--challenge-345/vinod-k/blog.txt1
-rw-r--r--challenge-345/vinod-k/perl/ch-1.pl39
-rw-r--r--challenge-345/vinod-k/perl/ch-2.pl65
3 files changed, 105 insertions, 0 deletions
diff --git a/challenge-345/vinod-k/blog.txt b/challenge-345/vinod-k/blog.txt
new file mode 100644
index 0000000000..aab67f206f
--- /dev/null
+++ b/challenge-345/vinod-k/blog.txt
@@ -0,0 +1 @@
+https://dev.to/vinodk89/perl-weekly-challenge-345-2bld
diff --git a/challenge-345/vinod-k/perl/ch-1.pl b/challenge-345/vinod-k/perl/ch-1.pl
new file mode 100644
index 0000000000..40983700d0
--- /dev/null
+++ b/challenge-345/vinod-k/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+###################################################################################
+#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.
+###################################################################################
+
+use strict;
+use warnings;
+
+sub find_peaks {
+ my @ints = @_;
+ my @peaks;
+ for my $i (0..$#ints) {
+ my $left = ($i == 0 ) ? -1 : $ints[$i-1];
+ my $right = ($i == $#ints) ? -1 : $ints[$i+1];
+ if ($ints[$i] > $left && $ints[$i] > $right) {
+ push @peaks, $i;
+ }
+ }
+ return @peaks;
+}
+
+# Examples
+my @examples = (
+ [1, 3, 2],
+ [2, 4, 6, 5, 3],
+ [1, 2, 3, 2, 4, 1],
+ [5, 3, 1],
+ [1, 5, 1, 5, 1, 5, 1],
+);
+
+foreach my $ex (@examples) {
+ my @peaks = find_peaks(@$ex);
+ print "Input: (@$ex)\n";
+ print "Output: (", join(", ", @peaks), ")\n\n";
+}
diff --git a/challenge-345/vinod-k/perl/ch-2.pl b/challenge-345/vinod-k/perl/ch-2.pl
new file mode 100644
index 0000000000..615150bc86
--- /dev/null
+++ b/challenge-345/vinod-k/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+###################################################################################
+#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.
+###################################################################################
+
+sub process_ints {
+ my @ints = @_;
+ my @seen;
+ my @ans;
+ my $run = 0;
+
+ for (my $i = 0; $i < @ints; $i++) {
+ if ($ints[$i] == -1) {
+ my $x = 0;
+ my $j = $i - 1;
+ while ($j >= 0 && $ints[$j] == -1) {
+ $x++;
+ $j--;
+ }
+ if ($x < @seen) {
+ push @ans, $seen[$x];
+ } else {
+ push @ans, -1;
+ }
+ } else {
+ unshift @seen, $ints[$i];
+ }
+ }
+ return @ans;
+}
+
+# Example
+my @examples = (
+ [5, -1, -1], # Output: (5, -1)
+ [3, 7, -1, -1, -1], # Output: (7, 3, -1)
+ [2, -1, 4, -1, -1], # Output: (2, 4, 2)
+ [10, 20, -1, 30, -1, -1], # Output: (20, 30, 20)
+ [-1, -1, 5, -1], # Output: (-1, -1, 5)
+);
+
+foreach my $ex (@examples) {
+ my @ans = process_ints(@$ex);
+ print "Input: (@$ex)\n";
+ print "Output: (", join(", ", @ans), ")\n\n";
+}