[Tutor] Sort a list into equal sized chunks
kent37 at tds.net
Sat Apr 16 15:22:04 CEST 2005
Klas Marteleur wrote:
> I have wrestled with a problem since last weeks knapsack discussion.
> This is what i want, but i cant get it into a program.
> I have a sorted list (for example):
> aList = [10,9,8,7,6,5,4,3,2,1]
> I have a max value (for example):
> I have another list:
> After processing "aList" i want my "anotherList" to look like this:
> were every element is as close to maxValue as possible.
As someone noted, this problem is properly called the bin packing problem. If you google for "bin
packing problem" there are a number of interesting references. This one is easy to read:
Finding the optimal partition is a hard problem. But for your purposes (sorting files onto DVDs) a
simple greedy algorithm might be sufficient. Here is a simple implementation of a First Fit
''' Partition a list into sublists whose sums don't exceed a maximum
using a First Fit Decreasing algorithm. See
for a simple description of the method.
''' Container for items that keeps a running sum '''
self.items = 
self.sum = 0
def append(self, item):
self.sum += item
''' Printable representation '''
return 'Bin(sum=%d, items=%s)' % (self.sum, str(self.items))
def pack(values, maxValue):
values = sorted(values, reverse=True)
bins = 
for item in values:
# Try to fit item into a bin
for bin in bins:
if bin.sum + item <= maxValue:
#print 'Adding', item, 'to', bin
# item didn't fit into any bin, start a new bin
#print 'Making new bin for', item
bin = Bin()
if __name__ == '__main__':
def packAndShow(aList, maxValue):
''' Pack a list into bins and show the result '''
print 'List with sum', sum(aList), 'requires at least', (sum(aList)+maxValue-1)/maxValue,
bins = pack(aList, maxValue)
print 'Solution using', len(bins), 'bins:'
for bin in bins:
aList = [10,9,8,7,6,5,4,3,2,1]
aList = [ random.randint(1, 11) for i in range(100) ]
More information about the Tutor