From f487deb4626b50b339f478245671b47bd50f64e6 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 23 Nov 2020 16:41:35 +0000 Subject: First pass at ch 88 --- challenge-088/james-smith/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-088/james-smith/perl/ch-2.pl | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 challenge-088/james-smith/perl/ch-1.pl create mode 100644 challenge-088/james-smith/perl/ch-2.pl 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; +} + -- cgit