aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-088/nunovieira220/perl/ch-1.pl22
-rw-r--r--challenge-088/nunovieira220/perl/ch-2.pl49
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-088/nunovieira220/perl/ch-1.pl b/challenge-088/nunovieira220/perl/ch-1.pl
new file mode 100644
index 0000000000..b0ee42c3df
--- /dev/null
+++ b/challenge-088/nunovieira220/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use List::Util qw(reduce);
+use Storable qw(dclone);
+use Data::Dumper::OneLine;
+
+# Input
+my @N = (5, 2, 1, 4, 3);
+
+# Array of Product
+my @res = ();
+
+for(my $i = 0; $i < scalar @N; $i++) {
+ my @aux = @{dclone(\@N)};
+ splice(@aux, $i, 1);
+ push @res, reduce { $a * $b } @aux;
+}
+
+# Output
+print Dumper(\@res); \ No newline at end of file
diff --git a/challenge-088/nunovieira220/perl/ch-2.pl b/challenge-088/nunovieira220/perl/ch-2.pl
new file mode 100644
index 0000000000..b56dcffa4c
--- /dev/null
+++ b/challenge-088/nunovieira220/perl/ch-2.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Data::Dumper::OneLine;
+
+# Input
+my @N = (
+ [ 1, 2, 3, 4],
+ [ 5, 6, 7, 8],
+ [ 9, 10, 11, 12],
+ [13, 14, 15, 16]
+);
+
+# Spiral Matrix
+my @result = handleMatrix(@N);
+
+# Output
+print Dumper(\@result);
+
+# Handle matrix
+sub handleMatrix {
+ my @matrix = @_;
+ my @res = ();
+
+ return @res if(scalar @matrix == 0);
+
+ my $firstRow = splice(@matrix, 0, 1);
+ push @res, @{$firstRow};
+
+ if(scalar @matrix > 0) {
+ for(my $i = 0; $i < scalar @matrix - 1; $i++) {
+ my $lastElem = splice(@{$matrix[$i]}, scalar(@{$matrix[$i]}) - 1, 1);
+ push @res, $lastElem;
+ }
+
+ my $lastRow = splice(@matrix, scalar(@matrix) - 1, 1);
+ push @res, reverse @{$lastRow};
+
+ for(my $i = scalar @matrix - 1; $i >= 0; $i--) {
+ my $firstElem = splice(@{$matrix[$i]}, 0, 1);
+ push @res, $firstElem;
+ }
+ }
+
+ push @res, handleMatrix(@matrix);
+
+ return @res;
+} \ No newline at end of file