diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-29 22:41:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-29 22:41:20 +0000 |
| commit | 479461a8aefc14de8f68b258b603e3a87634a080 (patch) | |
| tree | 7e3f7a071073b598986b74564cd35badd38b6ef2 /challenge-088 | |
| parent | 997f3befe5ec5e5e0f45b6f22babf8823e835f4f (diff) | |
| parent | 2c27c301d3a490bcdd20a3f390b5da053cda8531 (diff) | |
| download | perlweeklychallenge-club-479461a8aefc14de8f68b258b603e3a87634a080.tar.gz perlweeklychallenge-club-479461a8aefc14de8f68b258b603e3a87634a080.tar.bz2 perlweeklychallenge-club-479461a8aefc14de8f68b258b603e3a87634a080.zip | |
Merge pull request #2876 from ccntrq/challenge-088
add perl solutions for challenge-088:
Diffstat (limited to 'challenge-088')
| -rw-r--r-- | challenge-088/alexander-pankoff/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-088/alexander-pankoff/perl/ch-2.pl | 39 |
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-088/alexander-pankoff/perl/ch-1.pl b/challenge-088/alexander-pankoff/perl/ch-1.pl new file mode 100644 index 0000000000..bd25fb4e99 --- /dev/null +++ b/challenge-088/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +use v5.20; +use utf8; +use strict; +use warnings; +use feature qw(say signatures); +no warnings 'experimental::signatures'; + +use List::Util qw(product); + +{ + my @N = ( 5, 2, 1, 4, 3 ); + my @M = array_of_product(@N); + say '(' . join( ', ', @M ) . ')'; +} + +sub array_of_product(@arr) { + + # we first calculate the product of all array elements + my $total = product(@arr); + + # each item in the output is then the result of factoring out the + # corresponding element from the input list from that total + my @out = map { $total / $_ } @arr; + + return @out; +} diff --git a/challenge-088/alexander-pankoff/perl/ch-2.pl b/challenge-088/alexander-pankoff/perl/ch-2.pl new file mode 100644 index 0000000000..0c856723c3 --- /dev/null +++ b/challenge-088/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +use v5.20; +use utf8; +use strict; +use warnings; +use feature qw(say signatures); +no warnings 'experimental::signatures'; + +{ + my $MATRIX = [ + [ 1, 2, 3, 4 ], # + [ 5, 6, 7, 8 ], # + [ 9, 10, 11, 12 ], # + [ 13, 14, 15, 16 ], # + ]; + + say join( ', ', spriral_matrix($MATRIX) ) +} + +sub spriral_matrix($matrix) { + my @matrix = @$matrix; + + return () if !@matrix; + + # get top and bottom row an remove them from the input + my ( $top, $bot ); + ( $top, @matrix ) = @$matrix; + ( $bot, @matrix ) = ( $matrix[-1], @matrix[ 0 ... ( $#matrix - 1 ) ] ); + + # get left and right side from the remaining rows + my @left_side = map { $_->[0] } @matrix; + my @right_side = map { $_->[-1] } @matrix; + + # remove left and right side from the matrix + @matrix = map { [ @{$_}[ 1 ... ( $#$_ - 1 ) ] ] } @matrix; + + return ( @$top, @right_side, reverse( @{ $bot // [] } ), + reverse(@left_side), spriral_matrix( \@matrix ) ); +} |
