aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2020-11-29 13:57:44 -0500
committerAdam Russell <ac.russell@live.com>2020-11-29 13:57:44 -0500
commit42de3429122f018e501f84453cd40983e0541c98 (patch)
tree6b3d62d16936ce5dc7ec77cc4a43fda044b01e0e
parent7c0dd58d84501ef4445a2d50e6109e456d6512be (diff)
downloadperlweeklychallenge-club-42de3429122f018e501f84453cd40983e0541c98.tar.gz
perlweeklychallenge-club-42de3429122f018e501f84453cd40983e0541c98.tar.bz2
perlweeklychallenge-club-42de3429122f018e501f84453cd40983e0541c98.zip
Perl solutions and blog for challenge 088.
-rw-r--r--challenge-088/adam-russell/blog.txt1
-rw-r--r--challenge-088/adam-russell/perl/ch-1.pl30
-rw-r--r--challenge-088/adam-russell/perl/ch-2.pl70
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-088/adam-russell/blog.txt b/challenge-088/adam-russell/blog.txt
new file mode 100644
index 0000000000..12e2892ddf
--- /dev/null
+++ b/challenge-088/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2020/11/29
diff --git a/challenge-088/adam-russell/perl/ch-1.pl b/challenge-088/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..9f5394cc86
--- /dev/null
+++ b/challenge-088/adam-russell/perl/ch-1.pl
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+##
+# You are given an array of positive integers @N.
+# Write a script to return an array @M where $M[i]
+# is the product of all elements of @N except the index $N[i].
+##
+sub list_product{
+ my @numbers = @_;
+ my $product = 1;
+ map {$product *= $_ } @numbers;
+ return $product;
+}
+
+MAIN:{
+ my(@N, @M);
+ @N = (5, 2, 1, 4, 3);
+ for my $i (0 .. (@N - 1)){
+ my @numbers = @N[0 .. $i - 1, $i+1 .. (@N - 1)];
+ push @M, list_product(@numbers);
+ }
+ print "(" . join(", ", @M) . ")\n";
+ @M = ();
+ @N = (2, 1, 4, 3);
+ for my $i (0 .. (@N - 1)){
+ my @numbers = @N[0 .. $i - 1, $i+1 .. (@N - 1)];
+ push @M, list_product(@numbers);
+ }
+ print "(" . join(", ", @M) . ")\n";
+} \ No newline at end of file
diff --git a/challenge-088/adam-russell/perl/ch-2.pl b/challenge-088/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..4b2706aba4
--- /dev/null
+++ b/challenge-088/adam-russell/perl/ch-2.pl
@@ -0,0 +1,70 @@
+use strict;
+use warnings;
+##
+# You are given m x n matrix of positive integers.
+# Write a script to print spiral matrix as a list.
+##
+sub print_remove_top{
+ my(@matrix) = @_;
+ print join(", ", @{$matrix[0]}) . ", ";
+ splice(@matrix, 0, 1);
+ return @matrix;
+}
+
+sub print_remove_right{
+ my(@matrix) = @_;
+ my @right;
+ for my $row (@matrix){
+ push @right, $row->[-1];
+ my @a = @{$row}[0 .. (@{$row} - 2)];
+ $row = \@a;
+ }
+ print join(", ", @right) . ", ";
+ return @matrix;
+}
+
+sub print_remove_bottom{
+ my(@matrix) = @_;
+ print join(", ", reverse(@{$matrix[-1]})) . ", ";
+ splice(@matrix, -1);
+ return @matrix;
+}
+
+sub print_remove_left{
+ my(@matrix) = @_;
+ my @left;
+ for my $row (@matrix){
+ push @left, $row->[0];
+ my @a = @{$row}[1 .. (@{$row} - 1)];
+ $row = \@a;
+ }
+ print join(", ", reverse(@left)) . ", ";
+ return @matrix;
+}
+
+sub spiral_print{
+ my(@matrix) = @_;
+ print "[";
+ {
+ @matrix = print_remove_top(@matrix) if @matrix;
+ @matrix = print_remove_right(@matrix) if @matrix;
+ @matrix = print_remove_bottom(@matrix) if @matrix;
+ @matrix = print_remove_left(@matrix) if @matrix;
+ redo if @matrix;
+ }
+ print "\b\b]\n";
+}
+
+MAIN:{
+ spiral_print(
+ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9]
+ );
+ spiral_print(
+ [ 1, 2, 3, 4],
+ [ 5, 6, 7, 8],
+ [ 9, 10, 11, 12],
+ [13, 14, 15, 16]
+ );
+} \ No newline at end of file