diff options
| author | Flavio Poletti <flavio@polettix.it> | 2020-09-18 18:26:02 +0200 |
|---|---|---|
| committer | Flavio Poletti <flavio@polettix.it> | 2020-09-18 18:26:02 +0200 |
| commit | 996f1f50b936f2f3417c82d22e9b3db7818087b7 (patch) | |
| tree | d1736528c406793f3a2ec6bd707ba59032c691e2 | |
| parent | efad9ec67e425da2e42983948d78b1a348e104ed (diff) | |
| download | perlweeklychallenge-club-996f1f50b936f2f3417c82d22e9b3db7818087b7.tar.gz perlweeklychallenge-club-996f1f50b936f2f3417c82d22e9b3db7818087b7.tar.bz2 perlweeklychallenge-club-996f1f50b936f2f3417c82d22e9b3db7818087b7.zip | |
Solutions to PWC078 by Flavio Poletti (polettix)
| -rw-r--r-- | challenge-078/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-078/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-078/polettix/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-078/polettix/perl/ch-2.pl | 25 |
4 files changed, 60 insertions, 0 deletions
diff --git a/challenge-078/polettix/blog.txt b/challenge-078/polettix/blog.txt new file mode 100644 index 0000000000..ebf4d89d72 --- /dev/null +++ b/challenge-078/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2020/09/18/pwc078-leader-element/ diff --git a/challenge-078/polettix/blog1.txt b/challenge-078/polettix/blog1.txt new file mode 100644 index 0000000000..fd3ac19e56 --- /dev/null +++ b/challenge-078/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2020/09/19/pwc078-left-rotation/ diff --git a/challenge-078/polettix/perl/ch-1.pl b/challenge-078/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..eeb3f6d3e8 --- /dev/null +++ b/challenge-078/polettix/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; + +# This problem is easier to tackle if moving from the *end* of the array +# back to the beginning. So, we reverse the input array to analyze it +# and then reverse it again to get back to the original order. +sub keep_leaders { # @A <=> @_ + return (0) unless @_; + my $last_leader = $_[-1] - 1; + return reverse grep { + my $condition = $_ > $last_leader; + $last_leader = $_ if $condition; + $condition; + } reverse @_; +} + + +# testing stuff +for my $Aref ( + [9, 10, 7, 5, 6, 1], + [3, 4, 5], + [], +) { + + printout('Input: @A = ', @$Aref); + printout('Output: ', keep_leaders(@$Aref)); +} + +sub printout { + my $prefix = shift; + say $prefix, '(', join(', ', @_), ')' +} diff --git a/challenge-078/polettix/perl/ch-2.pl b/challenge-078/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..3328138672 --- /dev/null +++ b/challenge-078/polettix/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; + +sub shift_left_by ($n, @A) { (@A[$n..$#A], @A[0..($n-1)]) } +sub shift_left ($A, $B) { map { [shift_left_by($_, $A->@*)] } $B->@* } + +for my $test ( + [ + 'first test', + [qw< 10 20 30 40 50 >], + [qw< 3 4 >], + ], + [ + 'second test', + [qw< 7 4 2 6 3 >], + [qw< 1 3 4 >] + ], +) { + my ($title, $A, $B) = $test->@*; + say {*STDERR} $title; + say {*STDOUT} '[', join(', ', $_->@*), ']' for shift_left($A, $B); +} |
