aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@users.noreply.github.com>2025-10-31 17:48:37 +0100
committerGitHub <noreply@github.com>2025-10-31 17:48:37 +0100
commitbded1c25973b0d44285360cb69730eb30d7f9fc4 (patch)
tree85451ca4a2330bb0327240e72ba31628736a90bc
parent88a930c6a8de82b2c59f19958b4e1493ede4a4e0 (diff)
downloadperlweeklychallenge-club-bded1c25973b0d44285360cb69730eb30d7f9fc4.tar.gz
perlweeklychallenge-club-bded1c25973b0d44285360cb69730eb30d7f9fc4.tar.bz2
perlweeklychallenge-club-bded1c25973b0d44285360cb69730eb30d7f9fc4.zip
Create ch-1.pl
-rw-r--r--challenge-345/wanderdoc/perl/ch-1.pl68
1 files changed, 68 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;
+}