aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2025-10-27 20:04:52 +0100
committerThomas Köhler <jean-luc@picard.franken.de>2025-10-27 20:04:52 +0100
commit9c07a64b6301a498b190f2acdc63a34a9c000b9f (patch)
tree126503bf602081361400ecfa955aa19414acdf53
parentabe996e9ac2cb8caef4b993c2135a7f0d14bf455 (diff)
downloadperlweeklychallenge-club-9c07a64b6301a498b190f2acdc63a34a9c000b9f.tar.gz
perlweeklychallenge-club-9c07a64b6301a498b190f2acdc63a34a9c000b9f.tar.bz2
perlweeklychallenge-club-9c07a64b6301a498b190f2acdc63a34a9c000b9f.zip
Add solution 345.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-345/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-345/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-345/jeanluc2020/perl/ch-1.pl74
-rwxr-xr-xchallenge-345/jeanluc2020/perl/ch-2.pl105
4 files changed, 181 insertions, 0 deletions
diff --git a/challenge-345/jeanluc2020/blog-1.txt b/challenge-345/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..c5a0dd66cc
--- /dev/null
+++ b/challenge-345/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-345-1.html
diff --git a/challenge-345/jeanluc2020/blog-2.txt b/challenge-345/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..98bc64bc4c
--- /dev/null
+++ b/challenge-345/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-345-2.html
diff --git a/challenge-345/jeanluc2020/perl/ch-1.pl b/challenge-345/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..b620b26c09
--- /dev/null
+++ b/challenge-345/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-345/#TASK1
+#
+# Task 1: Peak Positions
+# ======================
+#
+# 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)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We walk from the beginning of the array to the end and check
+# if the current element is greater than its neighbors. If so,
+# we keep its index around to add it to the result.
+
+use v5.36;
+
+peak_positions(1, 3, 2);
+peak_positions(2, 4, 6, 5, 3);
+peak_positions(1, 2, 3, 2, 4, 1);
+peak_positions(5, 3, 1);
+peak_positions(1, 5, 1, 5, 1, 5, 1);
+
+sub peak_positions(@ints) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my @result = ();
+ foreach my $i (0..$#ints) {
+ my $keep = 1;
+ if($i > 0) {
+ $keep = 0 if $ints[$i] <= $ints[$i-1];
+ }
+ if($i < $#ints) {
+ $keep = 0 if $ints[$i] <= $ints[$i+1];
+ }
+ push @result, $i if $keep;
+ }
+ say "Output: (" . join(", ", @result) . ")";
+}
diff --git a/challenge-345/jeanluc2020/perl/ch-2.pl b/challenge-345/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..1e2810017b
--- /dev/null
+++ b/challenge-345/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,105 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-345/#TASK2
+#
+# Task 2: Last Visitor
+# ====================
+#
+# 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)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We just implement the rules as layed out in the exercise.
+# We use unshift to add at the beginning of @seen if needed,
+# otherwise we check our $x and either add $seen[$x] to @ans
+# or we add -1. Don't forget to increment $x of course. And
+# reset it to 0 if the number we're looking at is > 0.
+
+use v5.36;
+
+
+last_visitor(5, -1, -1);
+last_visitor(3, 7, -1, -1, -1);
+last_visitor(2, -1, 4, -1, -1);
+last_visitor(10, 20, -1, 30, -1, -1);
+last_visitor(-1, -1, 5, -1);
+
+sub last_visitor(@ints) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my @seen = ();
+ my @ans = ();
+ my $x = 0;
+ foreach my $elem (@ints) {
+ if($elem > 0) {
+ $x = 0;
+ unshift @seen, $elem;
+ } else {
+ if($x < scalar(@seen)) {
+ push @ans, $seen[$x];
+ } else {
+ push @ans, -1;
+ }
+ $x++;
+ }
+ }
+ say "Output: (" . join(", ", @ans) . ")";
+}