aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Miguel Prellezo Gutiérrez <niceperl@gmail.com>2020-11-25 20:26:04 +0100
committerJosé Miguel Prellezo Gutiérrez <niceperl@gmail.com>2020-11-25 20:26:04 +0100
commite5f1502c78a892f9db02fc81f4b7b1bf97fce735 (patch)
tree2c722fca6484b4e6259a9f6618e5e26b1b40e784
parent89360d10a5f2c28dfaa7524a737129ebe9dbd23a (diff)
downloadperlweeklychallenge-club-e5f1502c78a892f9db02fc81f4b7b1bf97fce735.tar.gz
perlweeklychallenge-club-e5f1502c78a892f9db02fc81f4b7b1bf97fce735.tar.bz2
perlweeklychallenge-club-e5f1502c78a892f9db02fc81f4b7b1bf97fce735.zip
Solutions to 088 challenge
-rw-r--r--challenge-088/miguel-prz/perl/Task088_1.pm21
-rw-r--r--challenge-088/miguel-prz/perl/Task088_2.pm74
-rw-r--r--challenge-088/miguel-prz/perl/ch-1.pl20
-rw-r--r--challenge-088/miguel-prz/perl/ch-2.pl34
4 files changed, 149 insertions, 0 deletions
diff --git a/challenge-088/miguel-prz/perl/Task088_1.pm b/challenge-088/miguel-prz/perl/Task088_1.pm
new file mode 100644
index 0000000000..9e0fac9f58
--- /dev/null
+++ b/challenge-088/miguel-prz/perl/Task088_1.pm
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+
+#------------------------------------------------------------------------------
+
+sub array_of_product {
+ my @result;
+ for( my $i=0; $i<@_; $i++ ) {
+ my $product = 1;
+ for( my $j=0; $j<@_; $j++ ) {
+ next if $i == $j;
+ $product *= $_[$j];
+ }
+ push @result, $product;
+ }
+ return @result;
+}
+
+#------------------------------------------------------------------------------
+
+1970;
diff --git a/challenge-088/miguel-prz/perl/Task088_2.pm b/challenge-088/miguel-prz/perl/Task088_2.pm
new file mode 100644
index 0000000000..75ee15dd0f
--- /dev/null
+++ b/challenge-088/miguel-prz/perl/Task088_2.pm
@@ -0,0 +1,74 @@
+use strict;
+use warnings;
+
+#------------------------------------------------------------------------------
+
+=pod
+
+Surround the array and consider:
+* 1: node was visited
+* 0: node has to be visited
+
+Example 6x4 matrix => converted to 8x6 (matrix_aux)
+
+1 1 1 1 1 1
+1 0 0 0 0 1
+1 0 0 0 0 1
+1 0 0 0 0 1
+1 0 0 0 0 1
+1 0 0 0 0 1
+1 0 0 0 0 1
+1 1 1 1 1 1
+
+Algorithm idea:
+- move in the current direction, visiting "0" nodes
+- change "0" node by "1"
+- when "1" node is reached, go back and change direction
+- if all nodes are visited, end
+
+=cut
+
+#------------------------------------------------------------------------------
+
+sub spiral_matrix {
+ my @matrix = @_;
+ my $aux_matrix = [];
+ my @result = ();
+
+ my $size_x = scalar $matrix[0]->@*;
+ my $size_y = scalar @matrix;
+ my $nodes = $size_x * $size_y;
+
+ push $aux_matrix->@*, [ (1) x ($size_x+2) ];
+ push $aux_matrix->@*, [ 1, (0) x $size_x, 1 ] for ( 1 .. $size_y );
+ push $aux_matrix->@*, [ (1) x ($size_x+2) ];
+
+ my $direction = 0;
+ my $visits = 0;
+ my ($cx, $cx_1) = (0, 0);
+ my ($cy, $cy_1) = (1, 1);
+
+ while( $visits < $nodes ) {
+
+ $direction == 0 && $cx++;
+ $direction == 1 && $cy++;
+ $direction == 2 && $cx--;
+ $direction == 3 && $cy--;
+
+ if( $aux_matrix->[$cy][$cx] ) {
+ $direction = ++$direction % 4;
+ ($cx, $cy) = ($cx_1, $cy_1);
+ }
+ else {
+ $aux_matrix->[$cy][$cx] = 1;
+ ($cx_1, $cy_1) = ($cx, $cy);
+ push @result, $matrix[$cy-1][$cx-1];
+ $visits++;
+ }
+ }
+ return @result;
+}
+
+#------------------------------------------------------------------------------
+
+1970;
diff --git a/challenge-088/miguel-prz/perl/ch-1.pl b/challenge-088/miguel-prz/perl/ch-1.pl
new file mode 100644
index 0000000000..85402214fb
--- /dev/null
+++ b/challenge-088/miguel-prz/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use Test::More;
+
+#------------------------------------------------------------------------------
+
+require_ok './Task088_1.pm';
+
+my @tests = (
+ { DATA=> [5, 2, 1, 4, 3], EXPECTED=> [24, 60, 120, 30, 40] },
+ { DATA=> [2, 1, 4, 3], EXPECTED=> [12, 24, 6, 8] },
+);
+
+for my $t( @tests ) {
+ my @data = $t->{DATA}->@*;
+ my @result = array_of_product (@data);
+ is_deeply $t->{EXPECTED}, \@result, "(@data) == (@result)";
+}
+
+done_testing; \ No newline at end of file
diff --git a/challenge-088/miguel-prz/perl/ch-2.pl b/challenge-088/miguel-prz/perl/ch-2.pl
new file mode 100644
index 0000000000..8ee3f201a8
--- /dev/null
+++ b/challenge-088/miguel-prz/perl/ch-2.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use Test::More;
+
+#------------------------------------------------------------------------------
+
+require_ok './Task088_2.pm';
+
+my @tests = (
+ {
+ DATA => [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ],
+ EXPECTED=> [ 1, 2, 3, 6, 9, 8, 7, 4, 5 ]
+ },
+ {
+ DATA=> [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ],
+ EXPECTED=> [ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ]
+ },
+ {
+ DATA=> [ [ 1, 2, 3, 4 , 5, 6, 7, 8 ] ],
+ EXPECTED=> [ 1, 2, 3, 4 , 5, 6, 7, 8 ]
+ },
+ {
+ DATA=> [ [1], [2], [3], [4] , [5] ],
+ EXPECTED=> [ 1, 2, 3, 4 , 5 ]
+ },
+);
+
+for my $t( @tests ) {
+ my @data = $t->{DATA}->@*;
+ my @result = spiral_matrix (@data);
+ is_deeply $t->{EXPECTED}, \@result, "(@result)";
+}
+
+done_testing; \ No newline at end of file