diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-27 23:11:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-27 23:11:54 +0000 |
| commit | 76cb55b9ce411d48526325a8b6d8ee179d42b723 (patch) | |
| tree | cd71288b9232405ea3596d9e2137e36784fcfbc0 | |
| parent | bb68a27c32aadb8f9c48241f07d89e018671a410 (diff) | |
| parent | bd5affd6f2753739979a5f1010b0dc7bf352dacd (diff) | |
| download | perlweeklychallenge-club-76cb55b9ce411d48526325a8b6d8ee179d42b723.tar.gz perlweeklychallenge-club-76cb55b9ce411d48526325a8b6d8ee179d42b723.tar.bz2 perlweeklychallenge-club-76cb55b9ce411d48526325a8b6d8ee179d42b723.zip | |
Merge pull request #12936 from choroba/ech345
Add solutions to 345: Peak Positions & Last Visitor by E. Choroba
| -rwxr-xr-x | challenge-345/e-choroba/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-345/e-choroba/perl/ch-2.pl | 33 |
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-345/e-choroba/perl/ch-1.pl b/challenge-345/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..7e5c822262 --- /dev/null +++ b/challenge-345/e-choroba/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub peak_positions(@ints) { + my @peaks; + for my $i (0 .. $#ints) { + if (($i == 0 || $ints[ $i - 1 ] < $ints[$i]) + && ($i == $#ints || $ints[$i] > $ints[ $i + 1 ])) { + push @peaks, $i; + } + } + return @peaks +} + +use Test2::V0; +plan(5); + +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'; diff --git a/challenge-345/e-choroba/perl/ch-2.pl b/challenge-345/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..1b53b6b43c --- /dev/null +++ b/challenge-345/e-choroba/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub last_visitor(@ints) { + my @seen; + my @ans; + my $negative = 0; + for my $i (@ints) { + if ($i > 0) { + unshift @seen, $i; + $negative = 0; + } else { + if ($negative < @seen) { + push @ans, $seen[$negative]; + } else { + push @ans, -1; + } + ++$negative; + } + } + return @ans +} + +use Test2::V0; +plan(5); + +is [last_visitor(5, -1, -1)], [5, -1], 'Example 1'; +is [last_visitor(3, 7, -1, -1, -1)], [7, 3, -1], 'Example 2'; +is [last_visitor(2, -1, 4, -1, -1)], [2, 4, 2], 'Example 3'; +is [last_visitor(10, 20, -1, 30, -1, -1)], [20, 30, 20], 'Example 4'; +is [last_visitor(-1, -1, 5, -1)], [-1, -1, 5], 'Example 5'; |
