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; }

___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: Challenge: Twist On Bin Packing
by Limbic~Region (Chancellor) on Apr 08, 2006 at 00:55 UTC
    eric256,
    No such luck. As Anonymous Monk points out elsewhere in this thread, the test case to meet is a bin size of 10 and an input list of 1,1,1,2,2,3,4,6,7,7 for which your solution fails.

    Cheers - L~R