diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-09-15 08:39:39 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-09-16 17:41:37 +0200 |
| commit | a81f31c80c6ea04b77ef54cc79604fabcba6528d (patch) | |
| tree | 102ee17827fcaca7afae85611b50641bba9c77e2 | |
| parent | 25bb3f560c454b526b036f7d265b50ad1e817c3c (diff) | |
| download | perlweeklychallenge-club-a81f31c80c6ea04b77ef54cc79604fabcba6528d.tar.gz perlweeklychallenge-club-a81f31c80c6ea04b77ef54cc79604fabcba6528d.tar.bz2 perlweeklychallenge-club-a81f31c80c6ea04b77ef54cc79604fabcba6528d.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-078/jo-37/perl/ch-1.pl | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/challenge-078/jo-37/perl/ch-1.pl b/challenge-078/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..34a1cbdd96 --- /dev/null +++ b/challenge-078/jo-37/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +use Test2::V0; +use List::Util 'reduce'; + +# Viewing at the array from right to left. Every element that is larger +# than the current first leading element is leading too and is prepended +# to the list. Appending (0) to an empty list as requested, though this +# leads to ambiguous results. +sub leading { + (@{(reduce { + unshift @$a, $b if $b > ($a->[0] // '-inf'); + $a; + } [], reverse @_)}, (0) x !@_) ; +} + +is [leading 9, 10, 7, 5, 6, 1], [10, 7, 6, 1], 'first example'; +is [leading 3, 4, 5], [5], 'second example'; +is [leading], [0], 'no leader'; +is [leading -1, 0], [0], 'ambiguous result'; + +done_testing; |
