aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-02-23 05:04:19 +0000
committerdrbaggy <js5@sanger.ac.uk>2021-02-23 05:04:19 +0000
commit6fce46c86da21aa2e28fa353bc1c85c89cb9ee1c (patch)
tree479c78c37829ce498520d9d16bac87191919a609 /challenge-101/james-smith
parent378e963f8876d4751eb375555d9cc90738ab633d (diff)
downloadperlweeklychallenge-club-6fce46c86da21aa2e28fa353bc1c85c89cb9ee1c.tar.gz
perlweeklychallenge-club-6fce46c86da21aa2e28fa353bc1c85c89cb9ee1c.tar.bz2
perlweeklychallenge-club-6fce46c86da21aa2e28fa353bc1c85c89cb9ee1c.zip
tidied up layout
Diffstat (limited to 'challenge-101/james-smith')
-rw-r--r--challenge-101/james-smith/perl/ch-1.pl24
1 files changed, 13 insertions, 11 deletions
diff --git a/challenge-101/james-smith/perl/ch-1.pl b/challenge-101/james-smith/perl/ch-1.pl
index ac89a45459..18434b2b69 100644
--- a/challenge-101/james-smith/perl/ch-1.pl
+++ b/challenge-101/james-smith/perl/ch-1.pl
@@ -9,32 +9,34 @@ use Test::More;
print_spiral( 1 .. $_ ) foreach 1..30;
sub print_spiral {
- say join q( ), map { sprintf '%2d', $_ } @{$_} foreach @{pack_spiral(@_)};
+ say join q( ), map { sprintf '%2d', $_ } @{$_}
+ foreach @{pack_spiral(@_)};
say '';
}
sub pack_spiral {
- my( $rows, @in ) = ( 1, @_ );
+ my $rows = 1;
## Get the value for columns & rows which have the smallest gap
## but still multiply to size of array (we choose rows to be
## no greater than columns as printing is neater - but for no
## other reason...
- $rows = @in%$_ ? $rows : $_ foreach 2 .. sqrt @in;
+ $rows = @_%$_ ? $rows : $_ foreach 2 .. sqrt @_;
- my ($cols,$r,$c,@out) = (@in/$rows,$rows-1,-1);
+ my ($cols,$r,$c,@out) = (@_/$rows,$rows-1,-1);
## We start bottom left...
## because we use pre-inc we actually start 1 to the left of it!
## as we "jump" before depositing the entry of the array...
-
- while( $rows && $cols ) { ## Do we have anything else to do?
- $out[$r][++$c] = shift @in foreach 1..$cols--; # >>
- $out[--$r][$c] = shift @in foreach 1..--$rows; # ^^
- last unless $rows && $cols; ## Skip if we have finished here
- $out[$r][--$c] = shift @in foreach 1..$cols--; # <<
- $out[++$r][$c] = shift @in foreach 1..--$rows; # vv
+ ## Remember shift by default operates on @_;
+
+ while( @_ ) { # do until empty
+ $out[ $r ][ ++$c ] = shift foreach 1 .. $cols--; # >>
+ $out[ --$r ][ $c ] = shift foreach 1 .. --$rows; # ^^
+ last unless @_; # exit if empty
+ $out[ $r ][ --$c ] = shift foreach 1 .. $cols--; # <<
+ $out[ ++$r ][ $c ] = shift foreach 1 .. --$rows; # vv
}
return \@out;