aboutsummaryrefslogtreecommitdiff
path: root/challenge-088/james-smith/perl/ch-2.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-088/james-smith/perl/ch-2.pl')
-rw-r--r--challenge-088/james-smith/perl/ch-2.pl25
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-088/james-smith/perl/ch-2.pl b/challenge-088/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..3ea3d71715
--- /dev/null
+++ b/challenge-088/james-smith/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+
+is( "@{ spiral_matrix([1,2,3],[4,5,6],[7,8,9]) }", '1 2 3 6 9 8 7 4 5' );
+is( "@{ spiral_matrix([1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]) }", '1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10' );
+
+done_testing();
+
+sub spiral_matrix {
+ my @rows = @_;
+ my @res;
+ while(@rows) {
+ push @res, @{shift @rows};
+ push @res, pop @{$_} foreach grep { @{$_} } @rows;
+ push @res, reverse @{ pop @rows } if @rows; ## Make sure we have data!
+ push @res, shift @{$_} foreach grep { @{$_} } reverse @rows;
+ }
+ return \@res;
+}
+