aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-345/zapwai/perl/ch-1.pl24
-rw-r--r--challenge-345/zapwai/perl/ch-2.pl39
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-345/zapwai/perl/ch-1.pl b/challenge-345/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..2665d1cde0
--- /dev/null
+++ b/challenge-345/zapwai/perl/ch-1.pl
@@ -0,0 +1,24 @@
+use v5.38;
+
+sub proc(@ints) {
+ say "Input: \@ints = @ints";
+ my @peaks;
+ push @peaks, 0 if ($ints[0] > $ints[1]);
+ for my $i (1 .. $#ints-1) {
+ push @peaks, $i if ($ints[$i] > $ints[$i-1]
+ && $ints[$i] > $ints[$i+1]);
+ }
+ push @peaks, $#ints if ($ints[$#ints] > $ints[$#ints-1]);
+ say "Output: @peaks";
+}
+
+my @ints = (1,3,2);
+proc(@ints);
+@ints = (2, 4, 6, 5, 3);
+proc(@ints);
+@ints = (1, 2, 3, 2, 4, 1);
+proc(@ints);
+@ints = (5, 3, 1);
+proc(@ints);
+@ints = (1, 5, 1, 5, 1, 5, 1);
+proc(@ints);
diff --git a/challenge-345/zapwai/perl/ch-2.pl b/challenge-345/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..f2b96d16d7
--- /dev/null
+++ b/challenge-345/zapwai/perl/ch-2.pl
@@ -0,0 +1,39 @@
+use v5.38;
+
+sub proc(@ints) {
+ say "Input: \@ints = @ints";
+ my @seen;
+ my @ans;
+ my $xlen = 0;
+ my $xon = 0; # a flag that -1 is seen
+ for my $num (@ints) {
+ if ($num == -1) {
+ if ($xon) {
+ $xlen++;
+ } else {
+ $xlen = 1;
+ $xon = 1;
+ }
+ if ($xlen <= @seen) {
+ push @ans, $seen[$xlen-1];
+ } else {
+ push @ans, -1;
+ }
+ } else {
+ unshift @seen, $num;
+ $xon = 0;
+ }
+ }
+ say "Output: @ans";
+}
+
+my @ints = (5,-1,-1);
+proc(@ints);
+@ints = (3, 7, -1, -1, -1);
+proc(@ints);
+@ints = (2, -1, 4, -1, -1);
+proc(@ints);
+@ints = (10, 20, -1, 30, -1, -1);
+proc(@ints);
+@ints = (-1, -1, 5, -1);
+proc(@ints);