aboutsummaryrefslogtreecommitdiff
path: root/challenge-078
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-16 17:01:46 +0100
committerGitHub <noreply@github.com>2020-09-16 17:01:46 +0100
commit4a997e1e871199f47c7735ed4320bb6ff9892b33 (patch)
tree0117c8981632d248ab9069f093b75dd83de519df /challenge-078
parent381db693b7ace879b835d5baf1695ff63fcc897c (diff)
parent2cff9e59a8c4158eba0d879a616b5a2151adb6bd (diff)
downloadperlweeklychallenge-club-4a997e1e871199f47c7735ed4320bb6ff9892b33.tar.gz
perlweeklychallenge-club-4a997e1e871199f47c7735ed4320bb6ff9892b33.tar.bz2
perlweeklychallenge-club-4a997e1e871199f47c7735ed4320bb6ff9892b33.zip
Merge pull request #2310 from jo-37/contrib
Solutions to challenge 078
Diffstat (limited to 'challenge-078')
-rwxr-xr-xchallenge-078/jo-37/perl/ch-1.pl22
-rw-r--r--challenge-078/jo-37/perl/ch-2.pl26
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;