diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-06-06 10:19:37 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-06-06 10:19:37 +0100 |
| commit | 224c27877bbd99f1fba6fb38d6a4317f669ce106 (patch) | |
| tree | 6cff0ba480b077b16b4ae3f3330ff3bab5c17c88 /challenge-115/james-smith | |
| parent | b7ee3deaa2a05acca98eba97c01a762c58b82eae (diff) | |
| download | perlweeklychallenge-club-224c27877bbd99f1fba6fb38d6a4317f669ce106.tar.gz perlweeklychallenge-club-224c27877bbd99f1fba6fb38d6a4317f669ce106.tar.bz2 perlweeklychallenge-club-224c27877bbd99f1fba6fb38d6a4317f669ce106.zip | |
tidied up code and pass by ref
Diffstat (limited to 'challenge-115/james-smith')
| -rw-r--r-- | challenge-115/james-smith/perl/ch-1.pl | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/challenge-115/james-smith/perl/ch-1.pl b/challenge-115/james-smith/perl/ch-1.pl index 94269e06c7..ba5bed2831 100644 --- a/challenge-115/james-smith/perl/ch-1.pl +++ b/challenge-115/james-smith/perl/ch-1.pl @@ -6,21 +6,31 @@ use warnings; use feature qw(say); use Test::More; -is( biggest_even(4,1,7,6), 7614 ); -is( biggest_even(1,4,2,8), 8412 ); -is( biggest_even(1,0,2,6), 6210 ); -is( biggest_even(1,7,9,6), 9716 ); -is( biggest_even(1,7,3,5), '' ); +my @TESTS = ( + [[qw(4 1 7 6)], 7614], + [[qw(1 4 2 8)], 8412], + [[qw(1 0 2 6)], 6210], + [[qw(1 7 9 6)], 9716], + [[qw(1 7 3 5)], ''], + [[qw(1 7 3 8)], 7318], + [[qw(2 4 6 8)], 8642], +); -done_testing(); +is( biggest_even( $_->[0] ), $_->[1] ) foreach @TESTS; sub biggest_even { - my $ptr = my @digits = reverse sort @_; + my $ptr = my @digits = reverse sort @{$_[0]}; + ## Firstly grab the digits in reverse numerical order ## Keep looping backwards through the array until we ## find a digit which is even - if this is the case ## we move it to the back and return the list. - $digits[$ptr]&1 || return join'',@digits[0..$ptr-1,$ptr+1..$#digits,$ptr] while $ptr--; + + while( $ptr-- ) { + next if $digits[$ptr] & 1; ## Skip if odd... + return join '', + @digits[ 0..$ptr-1, $ptr+1..$#digits, $ptr ]; + } ## If we get to the start return 0 as there are no even digits! return ''; |
