aboutsummaryrefslogtreecommitdiff
path: root/challenge-053
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-03-23 19:05:53 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-03-23 19:05:53 +0000
commit2c1e54579bf8e261ea10d1e797d7e0b3a4ae8313 (patch)
treefc382aa0d826983d629115207b5aca0e12421e5e /challenge-053
parent79645b6e55398688779aee58045e8339966c881d (diff)
downloadperlweeklychallenge-club-2c1e54579bf8e261ea10d1e797d7e0b3a4ae8313.tar.gz
perlweeklychallenge-club-2c1e54579bf8e261ea10d1e797d7e0b3a4ae8313.tar.bz2
perlweeklychallenge-club-2c1e54579bf8e261ea10d1e797d7e0b3a4ae8313.zip
- Added Perl solution for Rotate Matrix.
Diffstat (limited to 'challenge-053')
-rw-r--r--challenge-053/mohammad-anwar/perl/ch-1.pl66
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-053/mohammad-anwar/perl/ch-1.pl b/challenge-053/mohammad-anwar/perl/ch-1.pl
new file mode 100644
index 0000000000..367e00ce7a
--- /dev/null
+++ b/challenge-053/mohammad-anwar/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+
+my $unit_tests = {
+ 90 => {
+ in => [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ],
+ out => [ [ 7, 4, 1 ], [ 8, 5, 2 ], [ 9, 6, 3 ] ],
+ },
+ 180 => {
+ in => [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ],
+ out => [ [ 9, 8, 7 ], [ 6, 5, 4 ], [ 3, 2, 1 ] ],
+ },
+ 270 => {
+ in => [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ],
+ out => [ [ 3, 6, 9 ], [ 2, 5, 8 ], [ 1, 4, 7 ] ],
+ },
+};
+
+foreach my $degree (sort { $a <=> $b } keys %$unit_tests) {
+ my $in = $unit_tests->{$degree}->{in};
+ my $out = $unit_tests->{$degree}->{out};
+ cmp_deeply( rotate_matrix($in, $degree),
+ $out,
+ "testing $degree rotation" );
+}
+
+done_testing;
+
+#
+#
+# METHODS
+
+sub rotate_matrix {
+ my ($matrix, $degree) = @_;
+
+ my $_matrix;
+ foreach my $i ( 1 .. int($degree/90) ) {
+ $_matrix = _rotate_matrix($matrix);
+ $matrix = $_matrix;
+ }
+
+ return $_matrix;
+}
+
+sub _rotate_matrix {
+ my ($matrix) = @_;
+
+ my $rows = @$matrix;
+ my $cols = @{$matrix->[0]};
+
+ my $_matrix = [];
+ foreach my $i ( 0 .. $rows-1 ) {
+ my $k = 2;
+ foreach my $j ( 0 .. $cols-1 ) {
+ $_matrix->[$i][$j] = $matrix->[$k][$i];
+ $k--;
+ }
+ }
+
+ return $_matrix;
+}