Last active
August 29, 2020 00:18
-
-
Save tommybutler/af3c2eb7e34fe3a9572d8cf0d8b07d26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use strict; | |
use warnings; | |
use 5.020; | |
my $cap = 10; | |
my @ints = | |
qw( | |
6.1 | |
5.4 | |
3.3 | |
2.7 | |
0.6 | |
0.6 | |
0.6 | |
0.6 | |
); | |
my @groups = ( [] ); | |
sub sum | |
{ | |
my $total = 0; | |
$total += $_ for @_; | |
return $total; | |
} | |
sub run | |
{ | |
while ( @ints ) | |
{ | |
my $candidate = shift @ints; | |
INNER: for my $group ( @groups ) | |
{ | |
if ( sum( @$group ) + $candidate <= $cap ) | |
{ | |
push( @$group, $candidate ); | |
$candidate = undef; | |
last INNER; | |
} | |
} | |
if ( defined $candidate ) | |
{ | |
push @groups, [ $candidate ]; | |
} | |
} | |
use Data::Dumper; | |
say Dumper \@groups; | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment