aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-26 21:34:57 +0000
committerGitHub <noreply@github.com>2020-11-26 21:34:57 +0000
commit965f3c9a789092c503ae247afe79e577fc61c2d2 (patch)
tree215d9851288bd235718c10b5c76b9427ebe31ae9
parent7e12ec84ba25873bf1f0bb594bf3c9148eed8acd (diff)
parent042f4cb4327399f9e29224bb01985a38b7d02e37 (diff)
downloadperlweeklychallenge-club-965f3c9a789092c503ae247afe79e577fc61c2d2.tar.gz
perlweeklychallenge-club-965f3c9a789092c503ae247afe79e577fc61c2d2.tar.bz2
perlweeklychallenge-club-965f3c9a789092c503ae247afe79e577fc61c2d2.zip
Merge pull request #2852 from juliodcs/juliodcs-week88
Juliodcs week88
-rw-r--r--challenge-088/juliodcs/perl/ch-1.pl29
-rw-r--r--challenge-088/juliodcs/perl/ch-2.pl105
2 files changed, 134 insertions, 0 deletions
diff --git a/challenge-088/juliodcs/perl/ch-1.pl b/challenge-088/juliodcs/perl/ch-1.pl
new file mode 100644
index 0000000000..17646c24d4
--- /dev/null
+++ b/challenge-088/juliodcs/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+use List::Util 'reduce';
+
+sub array_of_product(@n) {
+ [ map {
+ @{$_} == 0 ? 0 : reduce { $a * $b } @{$_}
+ }
+ map {
+ my $idx = $_;
+ [ map { $n[$_] } grep { $idx != $_ } 0 .. @n - 1 ]
+ } 0 .. @n - 1 ]
+}
+
+if (@ARGV > 0) {
+ say join q(, ), array_of_product(@ARGV)->@*;
+ exit 0;
+}
+
+use Test::More;
+
+is_deeply array_of_product(999), [0], 'One element returns 0';
+is_deeply array_of_product(999, 888), [888, 999], 'Two elements invert positions';
+is_deeply array_of_product(5, 2, 1, 4, 3), [24, 60, 120, 30, 40], 'Example 1';
+is_deeply array_of_product(2, 1, 4, 3), [12, 24, 6, 8], 'Example 2';
+
+done_testing;
diff --git a/challenge-088/juliodcs/perl/ch-2.pl b/challenge-088/juliodcs/perl/ch-2.pl
new file mode 100644
index 0000000000..32ca3083da
--- /dev/null
+++ b/challenge-088/juliodcs/perl/ch-2.pl
@@ -0,0 +1,105 @@
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+use Data::Dumper;
+
+sub spiral_matrix($matrix, $acc = []) {
+ return $acc if $matrix->@* == 0;
+ my @new_acc = ($acc->@*, (shift $matrix->@*)->@*);
+
+ spiral_matrix(rotator($matrix), \@new_acc);
+}
+
+sub rotator($matrix) {
+ return [] if $matrix->@* == 0;
+ my $w = $matrix->[0]->@*;
+
+ [map {my $i = $_;[ map {$_->[$i]} $matrix->@* ]} reverse 0 .. $w - 1]
+}
+
+if (@ARGV > 0) {
+ say join q(, ), Dumper spiral_matrix(\@ARGV);
+ exit 0;
+}
+
+use Test::More;
+
+is_deeply spiral_matrix([
+[ 1 ],
+]),
+[ 1 ],
+'One number';
+
+is_deeply spiral_matrix([
+[ 1, 2, 3 ],
+]),
+[ 1, 2, 3 ],
+'One row';
+
+is_deeply spiral_matrix([
+[ 1 ],
+[ 2 ],
+[ 3 ],
+]),
+[ 1, 2, 3 ],
+'One column';
+
+is_deeply spiral_matrix([
+[ 1, 2, 3 ],
+[ 1, 2, 3 ],
+]),
+[ 1, 2, 3, 3, 2, 1 ],
+'Two rows';
+
+is_deeply spiral_matrix([
+[ 1, 7 ],
+[ 2, 8 ],
+[ 3, 9 ],
+]),
+[ 1, 7, 8, 9, 3, 2 ],
+'Two columns';
+
+is_deeply spiral_matrix([
+[ 1, 2, 3 ],
+[ 1, 2, 3 ],
+[ 1, 2, 3 ],
+]),
+[ 1, 2, 3, 3, 3, 2, 1, 1, 2 ],
+'Three rows';
+
+is_deeply spiral_matrix([
+[ 1, 7, 4 ],
+[ 2, 8, 5 ],
+[ 3, 9, 6 ],
+]),
+[ 1, 7, 4, 5, 6, 9, 3, 2, 8 ],
+'Three columns';
+
+is_deeply spiral_matrix([
+[ 1, 2, 3, 0 ],
+[ 4, 5, 6, 0 ],
+[ 4, 9, 1, 0 ],
+[ 7, 8, 9, 1 ],
+]),
+[ 1, 2, 3, 0, 0, 0, 1, 9, 8, 7, 4, 4, 5, 6, 1, 9 ],
+'4x4 matrix';
+
+is_deeply spiral_matrix([
+[ 1, 2, 3 ],
+[ 4, 5, 6 ],
+[ 7, 8, 9 ],
+]),
+[ 1, 2, 3, 6, 9, 8, 7, 4, 5 ],
+'Example 1';
+
+is_deeply 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 ],
+'Example 2';
+
+done_testing;