diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-15 00:44:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-15 00:44:32 +0100 |
| commit | 7e6e8ba60c901508220bfcceb4c5138d308a0937 (patch) | |
| tree | 643a0f1ca9c271743bd97e4d267704c0f7da9d19 | |
| parent | 65c5d8f658da418959da9c3e0363fe38305c70fc (diff) | |
| parent | 1c6c4ef84a52a44163f9a8e0a670a007e781297a (diff) | |
| download | perlweeklychallenge-club-7e6e8ba60c901508220bfcceb4c5138d308a0937.tar.gz perlweeklychallenge-club-7e6e8ba60c901508220bfcceb4c5138d308a0937.tar.bz2 perlweeklychallenge-club-7e6e8ba60c901508220bfcceb4c5138d308a0937.zip | |
Merge pull request #2294 from jacoby/master
challenge 78
| -rwxr-xr-x | challenge-078/dave-jacoby/perl/ch-1.pl | 45 | ||||
| -rwxr-xr-x | challenge-078/dave-jacoby/perl/ch-2.pl | 54 |
2 files changed, 99 insertions, 0 deletions
diff --git a/challenge-078/dave-jacoby/perl/ch-1.pl b/challenge-078/dave-jacoby/perl/ch-1.pl new file mode 100755 index 0000000000..e9930eb65e --- /dev/null +++ b/challenge-078/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{ max }; + +# You are given an array @A containing distinct integers. + +# Write a script to find all leader elements in the array @A. +# Print (0) if none found. + +# Input: @A = (9, 10, 7, 5, 6, 1) +# Output: (10, 7, 6, 1) + +my @A; +@A = ( 9, 10, 7, 5, 6, 1 ); +my @o1 = leader_element(@A); +say join ', ', @A; +say join ', ', @o1; +say ''; + +# Input: @A = (3, 4, 5) +# Output: (5) + +@A = ( 3, 4, 5 ); +my @o2 = leader_element(@A); +say join ', ', @A; +say join ', ', @o2; +say ''; + +sub leader_element ( @arr ) { + my @output; + + while (@arr) { + my $max = max @arr; + push @output, $arr[0] if $max == $arr[0]; + shift @arr; + } + + push @output, 0 unless scalar @output; + return wantarray ? @output : \@output; +} diff --git a/challenge-078/dave-jacoby/perl/ch-2.pl b/challenge-078/dave-jacoby/perl/ch-2.pl new file mode 100755 index 0000000000..429a796e35 --- /dev/null +++ b/challenge-078/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +# 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. + +# and this is where you look into the examples to tell, +# because by description, I would've thought that you +# work again on the modified array, not starting over + +my $o1 = left_rotation( [ 10, 20, 30, 40, 50 ], [ 3, 4 ] ); +my $o2 = left_rotation( [ 7, 4, 2, 6, 3 ], [ 1, 3, 4 ] ); + +for my $o ( $o1, $o2 ) { + for my $r ( $o->@* ) { + print '['; + print join ' ', $r->@*; + say ']'; + } + say ''; +} + +# Example 1 +# [40 50 10 20 30] +# [50 10 20 30 40] + +# Example 2 +# [4 2 6 3 7] +# [6 3 7 4 2] +# [3 7 4 2 6] + +sub left_rotation ( $nums, $indices ) { + my @output; + for my $i ( $indices->@* ) { + my @new = $nums->@*; + for ( 1 .. $i ) { + push @new, shift @new; + } + push @output, [@new]; + } + + push @output, 0 unless scalar @output; + return wantarray ? @output : \@output; +} |
