From a81f31c80c6ea04b77ef54cc79604fabcba6528d Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 15 Sep 2020 08:39:39 +0200 Subject: Solution to task 1 --- challenge-078/jo-37/perl/ch-1.pl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 challenge-078/jo-37/perl/ch-1.pl 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; -- cgit