aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2020-09-15 16:51:22 +0200
committerE. Choroba <choroba@matfyz.cz>2020-09-15 16:51:22 +0200
commit80b24be57009c816f48773770b077b8ecb674124 (patch)
treea39b4de47c2d396297c54f661c6f5088b73613cd
parent62703ba4f77e74b9fab081ff9a823c4e26361341 (diff)
downloadperlweeklychallenge-club-80b24be57009c816f48773770b077b8ecb674124.tar.gz
perlweeklychallenge-club-80b24be57009c816f48773770b077b8ecb674124.tar.bz2
perlweeklychallenge-club-80b24be57009c816f48773770b077b8ecb674124.zip
Solve 078 Leader Element and Left Rotation by E. Choroba
-rwxr-xr-xchallenge-078/e-choroba/perl/ch-1.pl25
-rwxr-xr-xchallenge-078/e-choroba/perl/ch-2.pl22
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-078/e-choroba/perl/ch-1.pl b/challenge-078/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..ba8aac506b
--- /dev/null
+++ b/challenge-078/e-choroba/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub leader_element {
+ my ($list) = @_;
+ return [] unless @$list;
+
+ my @leaders = $list->[-1];
+ for my $n (reverse @$list) {
+ unshift @leaders, $n if $n > $leaders[0];
+ }
+
+ return \@leaders
+}
+
+use Test::More tests => 2;
+
+is_deeply leader_element([9, 10, 7, 5, 6, 1]),
+ [10, 7, 6, 1],
+ 'example 1';
+
+is_deeply leader_element([3, 4, 5]),
+ [5],
+ 'example 2';
diff --git a/challenge-078/e-choroba/perl/ch-2.pl b/challenge-078/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..606e94c2a6
--- /dev/null
+++ b/challenge-078/e-choroba/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub left_rotation {
+ my ($list, $rotations) = @_;
+ return [ map {
+ [ @$list[$_ .. $#$list], @$list[0 .. $_ - 1] ]
+ } @$rotations ]
+}
+
+use Test::More tests => 2;
+
+is_deeply left_rotation([10, 20, 30, 40, 50], [3, 4]),
+ [ [40, 50, 10, 20, 30],
+ [50, 10, 20, 30, 40] ];
+
+is_deeply left_rotation([ 7, 4, 2, 6, 3 ], [1, 3, 4]),
+ [ [ 4, 2, 6, 3, 7 ],
+ [ 6, 3, 7, 4, 2 ],
+ [ 3, 7, 4, 2, 6 ] ];
+