From e79051dfdc249fc79ec9cfd837aaadf4e7375e7e Mon Sep 17 00:00:00 2001 From: drbaggy Date: Wed, 4 Aug 2021 10:46:43 +0100 Subject: pushed slightly shorter version with better comments and more pre-prep... --- challenge-124/james-smith/perl/ch-2.pl | 16 ++++------------ 1 file 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; -- cgit