diff options
| author | drbaggy <js5@sanger.ac.uk> | 2020-11-23 16:41:35 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2020-11-23 16:41:35 +0000 |
| commit | f487deb4626b50b339f478245671b47bd50f64e6 (patch) | |
| tree | 76e8029377d963eba3b8e4054a9826c84e63e535 | |
| parent | cea83925f87e4bbe52ab8c1ce9a2e1b0f2d86611 (diff) | |
| download | perlweeklychallenge-club-f487deb4626b50b339f478245671b47bd50f64e6.tar.gz perlweeklychallenge-club-f487deb4626b50b339f478245671b47bd50f64e6.tar.bz2 perlweeklychallenge-club-f487deb4626b50b339f478245671b47bd50f64e6.zip | |
First pass at ch 88
| -rw-r--r-- | challenge-088/james-smith/perl/ch-1.pl | 19 | ||||
| -rw-r--r-- | challenge-088/james-smith/perl/ch-2.pl | 25 |
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-088/james-smith/perl/ch-1.pl b/challenge-088/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..117fff074d --- /dev/null +++ b/challenge-088/james-smith/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( "@{[ prod_list( qw(5 2 1 4 3) ) ]}", '24 60 120 30 40' ); +is( "@{[ prod_list( qw(2 1 4 3) ) ]}", '12 24 6 8' ); + +done_testing(); + +sub prod_list { + my $N = 1; + $N *= $_ foreach @_; + return map { $N/$_} @_; +} + diff --git a/challenge-088/james-smith/perl/ch-2.pl b/challenge-088/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..3ea3d71715 --- /dev/null +++ b/challenge-088/james-smith/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( "@{ spiral_matrix([1,2,3],[4,5,6],[7,8,9]) }", '1 2 3 6 9 8 7 4 5' ); +is( "@{ 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' ); + +done_testing(); + +sub spiral_matrix { + my @rows = @_; + my @res; + while(@rows) { + push @res, @{shift @rows}; + push @res, pop @{$_} foreach grep { @{$_} } @rows; + push @res, reverse @{ pop @rows } if @rows; ## Make sure we have data! + push @res, shift @{$_} foreach grep { @{$_} } reverse @rows; + } + return \@res; +} + |
