in reply to Challenge: Twist On Bin Packing
I'm not sure it is 100% always going to work, but it seems to ;)
use strict; use warnings; use Data::Dumper; my @packages = sort { $b <=> $a } (2,2,3,4,5,6,7); print Dumper(@packages); my @bins; while (@packages) { my @temp = shift @packages; while (@packages) { last if sum(@temp) + $packages[-1] > 10; my $next = pop @packages; push @temp, $next; } push @bins, [@temp]; } print Dumper(\@bins); sub sum { my $s = 0; $s += $_ for @_; return $s; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Challenge: Twist On Bin Packing
by Limbic~Region (Chancellor) on Apr 08, 2006 at 00:55 UTC |