aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-078/adam-russell/blog.txt1
-rw-r--r--challenge-078/adam-russell/perl/ch-1.pl35
-rw-r--r--challenge-078/adam-russell/perl/ch-2.pl50
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";
+ }
+}