From b8089c321ba367660b76992e2a1fbc2c798baefc Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Fri, 27 Nov 2020 00:32:46 +0000 Subject: Add nunovieira220 perl solution to challenge 088 --- challenge-088/nunovieira220/perl/ch-1.pl | 22 ++++++++++++++ challenge-088/nunovieira220/perl/ch-2.pl | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 challenge-088/nunovieira220/perl/ch-1.pl create mode 100644 challenge-088/nunovieira220/perl/ch-2.pl 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 -- cgit From ad23c66c6119e7cc50dcb9d64a3f07ef9f8bda1f Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Fri, 27 Nov 2020 00:33:00 +0000 Subject: Add nunovieira220 js solution to challenge 088 --- challenge-088/nunovieira220/js/ch-1.js | 13 +++++++++++ challenge-088/nunovieira220/js/ch-2.js | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-088/nunovieira220/js/ch-1.js create mode 100644 challenge-088/nunovieira220/js/ch-2.js 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 -- cgit