aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-08-04 09:25:25 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-08-04 09:25:25 +0100
commitb26cc56147a7cdf9cac8e7f1bc6fd708aadf7eaa (patch)
treef3655a83d3a9d4f7820c4b16a7fc3c2e562eab6a
parent255eb8afa4cbf142cd9eabc953733cd6b23e04c7 (diff)
downloadperlweeklychallenge-club-b26cc56147a7cdf9cac8e7f1bc6fd708aadf7eaa.tar.gz
perlweeklychallenge-club-b26cc56147a7cdf9cac8e7f1bc6fd708aadf7eaa.tar.bz2
perlweeklychallenge-club-b26cc56147a7cdf9cac8e7f1bc6fd708aadf7eaa.zip
tidied up code
-rw-r--r--challenge-124/james-smith/perl/ch-2.pl20
1 files changed, 10 insertions, 10 deletions
diff --git a/challenge-124/james-smith/perl/ch-2.pl b/challenge-124/james-smith/perl/ch-2.pl
index 6b360cd40d..7f36fe9e6b 100644
--- a/challenge-124/james-smith/perl/ch-2.pl
+++ b/challenge-124/james-smith/perl/ch-2.pl
@@ -34,23 +34,23 @@ sub match_teams {
##
## $best - stores the result!!
##
- ## $best->[0] = array of team 1 members
- ## $best->[1] = array of team 2 members
- ## $best->[2] = difference between scores
+ ## $best->[0] = difference between scores
+ ## $best->[1] = array of team 1 members
+ ## $best->[2] = array of team 2 members
my( $diff, @names ) = @_;
- separate( 1 + int( @names/2 ), [$diff], [], $diff, my $best = [], @names );
- return "Team 1: [@{$best->[0]}]; Team 2: [@{$best->[1]}]; difference $best->[2]";
+ separate( 1 + int( @names/2 ), [$diff], [], $diff, my $best = [1<<63], @names );
+ return "Team 1: [@{$best->[1]}]; Team 2: [@{$best->[2]}]; difference $best->[0]";
}
sub separate {
my( $maxsize, $team1, $team2, $diff, $be, @nums ) = @_;
unless(@nums) {
- if( !defined $be->[0] || $be->[2]>abs $diff ) {
- $be->[0] = $team1; ## If this is the first time we have got to the end of the list
- $be->[1] = $team2; ## OR we have got to the end of the list and have a better solution
- $be->[2] = abs $diff; ## store this in $be - can't just do $be = [ , , ] as this would
- } ## change the pointer and wouldn't be preserved....
+ ## If this is the first time we have got to the end of the list
+ ## OR we have got to the end of the list and have a better solution
+ ## store this in $be - can't just do $be = [ , , ] as this would
+ ## change the pointer and wouldn't be preserved....
+ @{$be}=(abs $diff, $team1,$team2) if $be->[0]>abs $diff;
return;
}
my $next = shift @nums; ## Get the next person and allocate to team 1 and/or team 2 depending