diff options
| author | KjetilS <kjetilskotheim@gmail.com> | 2025-10-30 17:46:44 +0100 |
|---|---|---|
| committer | KjetilS <kjetilskotheim@gmail.com> | 2025-10-30 17:46:44 +0100 |
| commit | 5f57a6ea5cb5a75ba3e4570b26f20ce7dd085ffc (patch) | |
| tree | 0726af8ea1ef440d148ebc0f5f5a204938340fdb | |
| parent | bbbc17b878bcbd5a77c66aa6a6c080be7c08222d (diff) | |
| download | perlweeklychallenge-club-5f57a6ea5cb5a75ba3e4570b26f20ce7dd085ffc.tar.gz perlweeklychallenge-club-5f57a6ea5cb5a75ba3e4570b26f20ce7dd085ffc.tar.bz2 perlweeklychallenge-club-5f57a6ea5cb5a75ba3e4570b26f20ce7dd085ffc.zip | |
https://theweeklychallenge.org/blog/perl-weekly-challenge-345/
| -rw-r--r-- | challenge-345/kjetillll/perl/ch-1.pl | 9 | ||||
| -rw-r--r-- | challenge-345/kjetillll/perl/ch-2.pl | 28 |
2 files changed, 37 insertions, 0 deletions
diff --git a/challenge-345/kjetillll/perl/ch-1.pl b/challenge-345/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..9ef8dece4a --- /dev/null +++ b/challenge-345/kjetillll/perl/ch-1.pl @@ -0,0 +1,9 @@ +sub peaks { + grep { ( $_[$_-1] // -Inf ) < $_[$_] > ( $_[$_+1] // -Inf ) } 0 .. $#_ +} +use Test::More tests => 5; +is_deeply [ peaks(1, 3, 2) ], [1]; +is_deeply [ peaks(2, 4, 6, 5, 3) ], [2]; +is_deeply [ peaks(1, 2, 3, 2, 4, 1) ], [2, 4]; +is_deeply [ peaks(5, 3, 1) ], [0]; +is_deeply [ peaks(1, 5, 1, 5, 1, 5, 1) ], [1, 3, 5]; diff --git a/challenge-345/kjetillll/perl/ch-2.pl b/challenge-345/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..6d514681c1 --- /dev/null +++ b/challenge-345/kjetillll/perl/ch-2.pl @@ -0,0 +1,28 @@ +sub f_straightforward { + my($x, @seen, @ans) = (0); + for( @_ ){ + if ( $_ >= 0 ) { + unshift @seen, $_; + $x = 0; + } + else { + push @ans, $x < @seen ? $seen[$x] : -1; + $x++ + } + } + @ans +} + +sub f_kinda_functional { + my($x, @seen) = (0); + grep defined, map $_ > 0 && [$x=0, unshift @seen, $_] ? undef : $x++ < @seen ? $seen[$x-1] : -1, @_ +} + +use Test::More tests => 2 * 5; +for my $f ( \&f_straightforward, \&f_kinda_functional ){ + is_deeply [ &$f(5, -1, -1) ], [ 5, -1 ]; + is_deeply [ &$f(3, 7, -1, -1, -1) ], [ 7, 3, -1 ]; + is_deeply [ &$f(2, -1, 4, -1, -1) ], [ 2, 4, 2 ]; + is_deeply [ &$f(10, 20, -1, 30, -1, -1) ], [ 20, 30, 20 ]; + is_deeply [ &$f(-1, -1, 5, -1) ], [ -1, -1, 5 ]; +} |
