diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-20 13:16:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-20 13:16:44 +0100 |
| commit | bd41ad79e5795818c6d1b43844a78a052c22dc0e (patch) | |
| tree | 0b8dc396778e2f6225de7fa4943e056e14690511 | |
| parent | bae936a4696dc4314489ce6b7bac87f227f2c852 (diff) | |
| parent | 41b86a57dba73eeeb6d0d759c179a60c84442d4b (diff) | |
| download | perlweeklychallenge-club-bd41ad79e5795818c6d1b43844a78a052c22dc0e.tar.gz perlweeklychallenge-club-bd41ad79e5795818c6d1b43844a78a052c22dc0e.tar.bz2 perlweeklychallenge-club-bd41ad79e5795818c6d1b43844a78a052c22dc0e.zip | |
Merge pull request #2329 from wanderdoc/master
Solutions to challenge-078.
| -rw-r--r-- | challenge-078/wanderdoc/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-078/wanderdoc/perl/ch-2.pl | 55 |
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-078/wanderdoc/perl/ch-1.pl b/challenge-078/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..0cb6b4dfac --- /dev/null +++ b/challenge-078/wanderdoc/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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. An element is leader if it is greater than all the elements to its right side. + +Example 1: Input: @A = (9, 10, 7, 5, 6, 1) Output: (10, 7, 6, 1) +Example 2: Input: @A = (3, 4, 5) Output: (5) +=cut + + + + +use Test::More; + + + +sub find_leaders +{ + my @arr = @_; + return @arr if scalar @arr == 1; + + my @leaders; + unshift @leaders, pop @arr; + + for my $element ( reverse @arr ) + { + if ( $element > $leaders[0] ) + { + unshift @leaders, $element; + } + } + return @leaders; +} + + + + + +is_deeply([find_leaders(9, 10, 7, 5, 6, 1)], [(10, 7, 6, 1)], 'Example 1'); +is_deeply([find_leaders(3, 4, 5)], [(5)], 'Example 2'); +done_testing();
\ No newline at end of file diff --git a/challenge-078/wanderdoc/perl/ch-2.pl b/challenge-078/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..2254f0ed38 --- /dev/null +++ b/challenge-078/wanderdoc/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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. + +Example 1: Input: @A = (10 20 30 40 50) @B = (3 4) + +Output: + [40 50 10 20 30] + [50 10 20 30 40] + +Example 2: Input: @A = (7 4 2 6 3) @B = (1 3 4) + + +Output: + [4 2 6 3 7] + [6 3 7 4 2] + [3 7 4 2 6] +=cut + +use Test::More; + + + + +sub rotate_multiple +{ + my ($aref, $idx) = @_; + my @output; + for my $n ( @$idx ) + { + my @copy = @$aref; + push @output, [ rotate_left([@copy], $n) ]; + } + return @output; +} + + + +sub rotate_left +{ + my ( $aref, $num) = @_; + return @$aref[ map { ($_ + $num) % @$aref} 0 .. $#$aref ]; +} + + + + +is_deeply([rotate_multiple([10, 20, 30, 40, 50], [3, 4])], + [[40, 50, 10, 20, 30], [50, 10, 20, 30, 40]], 'Example 1'); +is_deeply([rotate_multiple([7, 4, 2, 6, 3], [1, 3, 4])], + [[4, 2, 6, 3, 7], [6, 3, 7, 4, 2], [3, 7, 4, 2, 6]], 'Example 2'); +done_testing();
\ No newline at end of file |
