diff options
| -rw-r--r-- | challenge-124/james-smith/perl/ch-2.pl | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/challenge-124/james-smith/perl/ch-2.pl b/challenge-124/james-smith/perl/ch-2.pl index 2348c8b946..005a10e8f8 100644 --- a/challenge-124/james-smith/perl/ch-2.pl +++ b/challenge-124/james-smith/perl/ch-2.pl @@ -8,15 +8,30 @@ use Test::More; use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); -my @TESTS = ( - [ 0, 1 ], -); - -is( my_function($_->[0]), $_->[1] ) foreach @TESTS; - done_testing(); -sub my_function { - return 1; +match_teams( map { $_*10 } 1..10 ); +match_teams( qw(10 -15 20 30 -25 0 5 40 -5) ); + +sub match_teams { + my @n = @_; + my $diff = shift @n; + my $best = []; + separate( \@n, 1 + int(@n/2), [$diff], [], $diff, $best ); + say "Team 1: [@{$best->[0]}]; Team 2[@{$best->[1]}]; difference $best->[2]"; + sub separate { + my($nums,$maxsize,$team1,$team2,$diff,$be) = @_; + if(@{$nums}==0) { + unless( defined $be->[0] && abs $diff>=$be->[2] ) { + $be->[0] = $team1; + $be->[1] = $team2; + $be->[2] = abs $diff; + } + return; + } + my $next = shift @{$nums}; + separate( [@{$nums}], $maxsize, [@{$team1},$next], $team2, $diff+$next, $be ) if @{$team1} < $maxsize; + separate( [@{$nums}], $maxsize, $team1, [@{$team2},$next], $diff-$next, $be ) if @{$team2} < $maxsize; + } } |
