aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-115/james-smith/perl/ch-1.pl26
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 '';