aboutsummaryrefslogtreecommitdiff
path: root/challenge-124
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-08-04 10:46:43 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-08-04 10:46:43 +0100
commite79051dfdc249fc79ec9cfd837aaadf4e7375e7e (patch)
treefb6b0484eb2634ad2222f76cc8d88469c86eb038 /challenge-124
parentf82a2715578f081167a0b9869b42f318548740a4 (diff)
downloadperlweeklychallenge-club-e79051dfdc249fc79ec9cfd837aaadf4e7375e7e.tar.gz
perlweeklychallenge-club-e79051dfdc249fc79ec9cfd837aaadf4e7375e7e.tar.bz2
perlweeklychallenge-club-e79051dfdc249fc79ec9cfd837aaadf4e7375e7e.zip
pushed slightly shorter version with better comments and more pre-prep...
Diffstat (limited to 'challenge-124')
-rw-r--r--challenge-124/james-smith/perl/ch-2.pl16
1 files changed, 4 insertions, 12 deletions
diff --git a/challenge-124/james-smith/perl/ch-2.pl b/challenge-124/james-smith/perl/ch-2.pl
index 7f36fe9e6b..ba27205aae 100644
--- a/challenge-124/james-smith/perl/ch-2.pl
+++ b/challenge-124/james-smith/perl/ch-2.pl
@@ -4,11 +4,8 @@ use strict;
use warnings;
use feature qw(say);
-use Test::More;
use Benchmark qw(cmpthese timethis);
-use Data::Dumper qw(Dumper);
-done_testing();
say match_teams( map { $_*10 } 1..15 );
say match_teams( map { $_*10 } 1..10 );
say match_teams( qw(10 -15 20 30 -25 0 5 40 -5) );
@@ -16,7 +13,6 @@ say match_teams( qw(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 7
timethis(1000, sub { match_teams( map { $_*10 } 1..10 ); } );
timethis(1000, sub { match_teams( map { $_*10 } 1..12 ); } );
timethis(1000, sub { match_teams( map { $_*10 } 1..14 ); } );
-exit;
timethis(100, sub { match_teams( map { $_*10 } 1..16 ); } );
timethis(100, sub { match_teams( map { $_*10 } 1..18 ); } );
timethis(100, sub { match_teams( map { $_*10 } 1..20 ); } );
@@ -45,14 +41,10 @@ sub match_teams {
sub separate {
my( $maxsize, $team1, $team2, $diff, $be, @nums ) = @_;
- unless(@nums) {
- ## 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;
- }
+ return ( $be->[0] > abs $diff ) && ( @{$be} = ( abs $diff, $team1, $team2 ) ) unless @nums;
+ ## If we get to the end of the list and have a better solution (smaller diff)
+ ## store info in $be - can't just do $be = [ , , ] as this would
+ ## change the pointer and wouldn't be preserved....
my $next = shift @nums; ## Get the next person and allocate to team 1 and/or team 2 depending
## on whether the teams have space...
separate( $maxsize, [@{$team1},$next], $team2, $diff+$next, $be, @nums ) if @{$team1} < $maxsize;