diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-27 14:30:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-27 14:30:55 +0000 |
| commit | 9e0b45f5079ce689c62500d70642709a80bee0a5 (patch) | |
| tree | 7dd236fdd8f603b5210ff7f3842f14e21b2dafb2 | |
| parent | 793f3dca81f49f9365fd0a94ab6ad31eb2816dc4 (diff) | |
| parent | f6b775ceb35655572866795941576db2040d12b9 (diff) | |
| download | perlweeklychallenge-club-9e0b45f5079ce689c62500d70642709a80bee0a5.tar.gz perlweeklychallenge-club-9e0b45f5079ce689c62500d70642709a80bee0a5.tar.bz2 perlweeklychallenge-club-9e0b45f5079ce689c62500d70642709a80bee0a5.zip | |
Merge pull request #12930 from mahnkong/challenge-345
Challenge 345
| -rw-r--r-- | challenge-345/mahnkong/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-345/mahnkong/perl/ch-2.pl | 28 |
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-345/mahnkong/perl/ch-1.pl b/challenge-345/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..6f43d7438d --- /dev/null +++ b/challenge-345/mahnkong/perl/ch-1.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@ints) { + my @peaks; + for (my $i = 0; $i < scalar(@ints); $i++) { + if ((! exists $ints[$i-1] || $ints[$i] > $ints[$i-1]) && (! exists $ints[$i+1] || $ints[$i] > $ints[$i+1])) { + push @peaks, $i; + } + } + return [ @peaks ]; +} + +is_deeply(run(1,3,2), [1], "Example 1"); +is_deeply(run(2,4,6,5,3), [2], "Example 2"); +is_deeply(run(1,2,3,2,4,1), [2,4], "Example 3"); +is_deeply(run(5,3,1), [0], "Example 4"); +is_deeply(run(1,5,1,5,1,5,1), [1,3,5], "Example 5"); diff --git a/challenge-345/mahnkong/perl/ch-2.pl b/challenge-345/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..c3bd2ae793 --- /dev/null +++ b/challenge-345/mahnkong/perl/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@ints) { + my @seen; + my @ans; + my $streak = 0; + + foreach my $int (@ints) { + if ($int == -1) { + push @ans, $streak < scalar(@seen) ? $seen[$streak] : -1; + $streak += 1; + } else { + unshift @seen, $int if $int > 0; + $streak = 0; + } + } + + return [ @ans ]; +} + +is_deeply(run(5, -1, -1), [5, -1], "Example 1"); +is_deeply(run(3, 7, -1, -1, -1), [7, 3, -1], "Example 2"); +is_deeply(run(2, -1, 4, -1, -1), [2, 4, 2], "Example 3"); +is_deeply(run(10, 20, -1, 30, -1, -1), [20, 30, 20], "Example 4"); +is_deeply(run(-1, -1, 5, -1), [-1, -1, 5], "Example 5"); |
