diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-12 20:20:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-12 20:20:57 +0100 |
| commit | 33e6622fa1df9f5aac8f83e6e4ef48c9b9e461c4 (patch) | |
| tree | d4126e9119f5ec993a5f67aedd144948a71cab1d | |
| parent | 0a6af2f308d5afe102c9179091bac3edd90b09a0 (diff) | |
| parent | be6a93862f874a75e9fc8e82e179077146ac762a (diff) | |
| download | perlweeklychallenge-club-33e6622fa1df9f5aac8f83e6e4ef48c9b9e461c4.tar.gz perlweeklychallenge-club-33e6622fa1df9f5aac8f83e6e4ef48c9b9e461c4.tar.bz2 perlweeklychallenge-club-33e6622fa1df9f5aac8f83e6e4ef48c9b9e461c4.zip | |
Merge pull request #2070 from jo-37/contrib
Solutions for challenge 073
| -rwxr-xr-x | challenge-073/jo-37/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-073/jo-37/perl/ch-2.pl | 18 |
2 files changed, 36 insertions, 0 deletions
diff --git a/challenge-073/jo-37/perl/ch-1.pl b/challenge-073/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..67372bc6b5 --- /dev/null +++ b/challenge-073/jo-37/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use Test2::V0; +use List::Util 'min'; + +# first arg: window size +# remaining args: input array +sub sliding_min { + my $S = shift; + + # Take the minimum of a sliding window + map min(@_[$_ .. $_ + $S - 1]), 0 .. $#_ - $S + 1; +} + +is [sliding_min 3, (1, 5, 0, 2, 9, 3, 7, 6, 4, 8)], + [0, 0, 0, 2, 3, 3, 4, 4], 'example from challenge'; + +done_testing; diff --git a/challenge-073/jo-37/perl/ch-2.pl b/challenge-073/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..82c9b605c0 --- /dev/null +++ b/challenge-073/jo-37/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use Test2::V0; +use List::Util 'min'; + +sub left_min { + # For all array elements: + # Take the array slice "left" of the element, + # filter for elements smaller than the selected, + # take the mininum of those + # and set the result to zero if there was nothing to minimize. + map {my $e = $_[$_]; min(grep {$_ < $e} @_[0 .. $_-1]) // 0} 0 .. $#_; +} + +is [left_min 7, 8, 3, 12, 10], [0, 7, 0, 3, 3], 'first example'; +is [left_min 4, 6, 5], [0, 4, 4], 'second example'; + +done_testing; |
