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