diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-09-16 17:42:52 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-09-16 17:42:52 +0200 |
| commit | 2cff9e59a8c4158eba0d879a616b5a2151adb6bd (patch) | |
| tree | ac617629433f247606ee6801b21e525fed6624f6 | |
| parent | 25bb3f560c454b526b036f7d265b50ad1e817c3c (diff) | |
| parent | e2673327977639133d0715c39a4ff798050be36f (diff) | |
| download | perlweeklychallenge-club-2cff9e59a8c4158eba0d879a616b5a2151adb6bd.tar.gz perlweeklychallenge-club-2cff9e59a8c4158eba0d879a616b5a2151adb6bd.tar.bz2 perlweeklychallenge-club-2cff9e59a8c4158eba0d879a616b5a2151adb6bd.zip | |
Solutions to challenge 078
| -rwxr-xr-x | challenge-078/jo-37/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-078/jo-37/perl/ch-2.pl | 26 |
2 files changed, 48 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; diff --git a/challenge-078/jo-37/perl/ch-2.pl b/challenge-078/jo-37/perl/ch-2.pl new file mode 100644 index 0000000000..0b957f712c --- /dev/null +++ b/challenge-078/jo-37/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +use PDL; +# avoid conflicting import +use Test2::V0 '!float'; + +sub multi_rotate { + # convert 1st arg to a piddle + my $a = long(shift); + # 2nd arg: pack the array elements into arrays of length one. + my $b = [map [$_], @{(shift)}]; + + # The range method returns rectangular parts of the input piddle + # starting at the given positions. With the full length of $a as + # the parts' length and periodic boundary conditions it actually + # performs multiple rotations just as required by this task. The + # source dimension needs to be moved to the front. + $a->range($b, $a->dim(0), 'periodic')->reorder(1, 0)->unpdl; +} + +is multi_rotate([10, 20, 30, 40, 50], [3, 4]), + [[40, 50, 10, 20, 30], [50, 10, 20, 30, 40]], 'first example'; +is multi_rotate([7, 4, 2, 6, 3], [1, 3, 4]), + [[4, 2, 6, 3, 7], [6, 3, 7, 4, 2], [3, 7, 4, 2, 6]], 'second example'; + +done_testing; |
