diff options
| author | Alexander Pankoff <ccntrq@screenri.de> | 2020-09-14 09:07:57 +0200 |
|---|---|---|
| committer | Alexander Pankoff <ccntrq@screenri.de> | 2020-09-14 09:07:59 +0200 |
| commit | 6a6abb02362fcfbf45a834b59cf26369d77be6c8 (patch) | |
| tree | ed67c7b4afaf66a7fe7e6a4b5efb4554218e0dbe /challenge-078/alexander-pankoff | |
| parent | 5c70a9c510b543b6d65e14df896a0d5487c708bf (diff) | |
| download | perlweeklychallenge-club-6a6abb02362fcfbf45a834b59cf26369d77be6c8.tar.gz perlweeklychallenge-club-6a6abb02362fcfbf45a834b59cf26369d77be6c8.tar.bz2 perlweeklychallenge-club-6a6abb02362fcfbf45a834b59cf26369d77be6c8.zip | |
add solution for left rotation task from c-078
Diffstat (limited to 'challenge-078/alexander-pankoff')
| -rw-r--r-- | challenge-078/alexander-pankoff/perl/ch-2.pl | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-078/alexander-pankoff/perl/ch-2.pl b/challenge-078/alexander-pankoff/perl/ch-2.pl new file mode 100644 index 0000000000..f4dd950b48 --- /dev/null +++ b/challenge-078/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use v5.20; +use utf8; +use strict; +use warnings; +use autodie; +use feature qw(say signatures); +no warnings 'experimental::signatures'; + +# You are given array @A containing positive numbers and @B containing one or +# more indices from the array @A. +# +# Write a script to left rotate @A so that the number at the first index of @B +# becomes the first element in the array. Similary, left rotate @A again so +# that the number at the second index of @B becomes the first element in the +# array. + +my @A = ( 10, 20, 30, 40, 50 ); +my @B = ( 3, 4 ); + +for my $index (@B) { + say '[' . join( ', ', left_rotate( $index, @A ) ) . ']'; +} + +sub left_rotate ( $index, @array ) { + while ( $index-- ) { + my $top = shift @array; + push @array, $top; + } + + return @array; +} |
