diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-20 09:53:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-20 09:53:23 +0100 |
| commit | cd39857faed89acadc8624de9faa9bdd64d52871 (patch) | |
| tree | 61e5607fb87f6ed7320a5d4d20df5c6e127d418a | |
| parent | 15970b39e177415b2ead601c5491c89bf7ffcc77 (diff) | |
| parent | 0c88aab6353c87a6fa5c1314504699430c4c9d16 (diff) | |
| download | perlweeklychallenge-club-cd39857faed89acadc8624de9faa9bdd64d52871.tar.gz perlweeklychallenge-club-cd39857faed89acadc8624de9faa9bdd64d52871.tar.bz2 perlweeklychallenge-club-cd39857faed89acadc8624de9faa9bdd64d52871.zip | |
Merge pull request #2326 from adamcrussell/challenge-078
solutions for challenge 078
| -rw-r--r-- | challenge-078/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-078/adam-russell/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-078/adam-russell/perl/ch-2.pl | 50 |
3 files changed, 86 insertions, 0 deletions
diff --git a/challenge-078/adam-russell/blog.txt b/challenge-078/adam-russell/blog.txt new file mode 100644 index 0000000000..96ebb235d6 --- /dev/null +++ b/challenge-078/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/2020/09/20#pwc078 diff --git a/challenge-078/adam-russell/perl/ch-1.pl b/challenge-078/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..4f3c89428c --- /dev/null +++ b/challenge-078/adam-russell/perl/ch-1.pl @@ -0,0 +1,35 @@ +use strict; +use warnings; +## +# 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. +## +use boolean; +sub is_leader{ + my(@a) = @_; + my $l = shift @a; + for my $x (@a){ + return false if $x > $l; + } + return true; +} + +sub find_leaders{ + my(@a) = @_; + my @leaders; + for my $i (0 .. @a - 1){ + push @leaders, $a[$i] if !$a[$i + 1] || is_leader(@a[$i .. @a - 1]); + } + return @leaders; +} + +MAIN:{ + my @A; + @A = (9, 10, 7, 5, 6, 1); + print "\@A = (" . join(",", @A) . ")\n"; + print "Leaders = (" . join(",", find_leaders(@A)) . ")\n"; + @A = (3, 4, 5); + print "\@A = (" . join(",", @A) . ")\n"; + print "Leaders = (" . join(",", find_leaders(@A)) . ")\n"; +} diff --git a/challenge-078/adam-russell/perl/ch-2.pl b/challenge-078/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..eb7f647492 --- /dev/null +++ b/challenge-078/adam-russell/perl/ch-2.pl @@ -0,0 +1,50 @@ +use strict; +use warnings; +## +# 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. +## +sub rotate{ + my($a, $r) = @_; + my @rotated; + for(0 .. ($r - 1)){ + my $temp = shift @{$a}; + push @{$a}, $temp; + } + return $a; +} + +MAIN:{ + my(@A, @B); + @A = (10, 20, 30, 40, 50); + @B = (3, 4); + my @rotations; + for my $b (@B){ + my @temp = @A; + push @rotations, rotate(\@temp, $b); + } + print "\@A = (" . join(",", @A) . ")\n"; + print "\@B = (" . join(",", @B) . ")\n"; + print "Rotations:\n"; + for my $rotation (@rotations){ + print "\t[" . join(",", @{$rotation}) . "]\n"; + } + @rotations = (); + @A = (7, 4, 2, 6, 3); + @B = (1, 3, 4); + for my $b (@B){ + my @temp = @A; + push @rotations, rotate(\@temp, $b); + } + print "\@A = (" . join(",", @A) . ")\n"; + print "\@B = (" . join(",", @B) . ")\n"; + print "Rotations:\n"; + for my $rotation (@rotations){ + print "\t[" . join(",", @{$rotation}) . "]\n"; + } +} |
