aboutsummaryrefslogtreecommitdiff
path: root/challenge-124/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-08-02 10:57:33 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-08-02 10:57:33 +0100
commit6d12a38472aa39e44be069f9600f9aa87c63699c (patch)
tree15e0585a58bdf26dc8e72400df95206d6138899a /challenge-124/james-smith
parent16bc0dcb20a36d06296ef7de89c6c0f3a4da25dd (diff)
downloadperlweeklychallenge-club-6d12a38472aa39e44be069f9600f9aa87c63699c.tar.gz
perlweeklychallenge-club-6d12a38472aa39e44be069f9600f9aa87c63699c.tar.bz2
perlweeklychallenge-club-6d12a38472aa39e44be069f9600f9aa87c63699c.zip
first pass at ch-2
Diffstat (limited to 'challenge-124/james-smith')
-rw-r--r--challenge-124/james-smith/perl/ch-2.pl31
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;
+ }
}