diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-27 10:27:40 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-27 10:27:40 +0000 |
| commit | 7d09e99b5eb361e4d834c8a947e16eb414dcd4a5 (patch) | |
| tree | 740cbbaac45f1e5970453f3e14179b2bb9168fbb | |
| parent | 349bed1114462cf172825e17ac081c9e08549b80 (diff) | |
| parent | ad23c66c6119e7cc50dcb9d64a3f07ef9f8bda1f (diff) | |
| download | perlweeklychallenge-club-7d09e99b5eb361e4d834c8a947e16eb414dcd4a5.tar.gz perlweeklychallenge-club-7d09e99b5eb361e4d834c8a947e16eb414dcd4a5.tar.bz2 perlweeklychallenge-club-7d09e99b5eb361e4d834c8a947e16eb414dcd4a5.zip | |
Merge pull request #2854 from nunovieira220/challenge-088
Add solution to challenge 088
| -rw-r--r-- | challenge-088/nunovieira220/js/ch-1.js | 13 | ||||
| -rw-r--r-- | challenge-088/nunovieira220/js/ch-2.js | 40 | ||||
| -rw-r--r-- | challenge-088/nunovieira220/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-088/nunovieira220/perl/ch-2.pl | 49 |
4 files changed, 124 insertions, 0 deletions
diff --git a/challenge-088/nunovieira220/js/ch-1.js b/challenge-088/nunovieira220/js/ch-1.js new file mode 100644 index 0000000000..feb58d7db2 --- /dev/null +++ b/challenge-088/nunovieira220/js/ch-1.js @@ -0,0 +1,13 @@ +// Input +const N = [2, 1, 4, 3]; + +// Array of Product +const res = []; + +for(let i = 0; i < N.length; i++) { + const aux = N.filter((_, index) => i !== index); + res.push(aux.reduce((a, b) => a * b)); +} + +// Output +console.log(res);
\ No newline at end of file diff --git a/challenge-088/nunovieira220/js/ch-2.js b/challenge-088/nunovieira220/js/ch-2.js new file mode 100644 index 0000000000..87c256507f --- /dev/null +++ b/challenge-088/nunovieira220/js/ch-2.js @@ -0,0 +1,40 @@ +// Handle spiral matrix +const handleMatrix = (matrix) => { + let res = []; + + if(!matrix.length) return res; + + const firstRow = matrix.splice(0, 1)[0]; + res = res.concat(firstRow); + + if(matrix.length > 0) { + for(let i = 0; i < matrix.length - 1; i++) { + const lastElem = matrix[i].splice(matrix[i].length - 1, 1)[0]; + res.push(lastElem); + } + + const lastRow = matrix.splice(matrix.length - 1, 1)[0]; + res = res.concat(lastRow.reverse()); + + for(let i = matrix.length - 1; i >= 0; i--) { + const firstElem = matrix[i].splice(0, 1)[0]; + res.push(firstElem); + } + } + + return res.concat(handleMatrix(matrix)); +} + +// Input +const N = [ + [ 1, 2, 3, 4], + [ 5, 6, 7, 8], + [ 9, 10, 11, 12], + [13, 14, 15, 16] +]; + +// Spiral Matrix +const res = handleMatrix(N); + +// Output +console.log(res);
\ No newline at end of file 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 |
