[Tutor] Sort a list into equal sized chunks

Klas Marteleur klas.martelleur at telia.com
Sun Apr 17 18:45:21 CEST 2005


Thanks Kent
Your program does what i wanted to accomplish. But i dont really know why, and 
that disturbs me. 

I guess its the class that confuses me. Could you or sombody else on this list 
help me out by putting some words on what is happening in this program, and 
in which order things are done?

lördagen den 16 april 2005 15.22 skrev Kent Johnson:
> class Bin(object):
>      ''' Container for items that keeps a running sum '''
>      def __init__(self):
>          self.items = []
>          self.sum = 0
>
>      def append(self, item):
>          self.items.append(item)
>          self.sum += item
>
>      def __str__(self):
>          ''' 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
>                  bin.append(item)
>                  break
>          else:
>              # item didn't fit into any bin, start a new bin
>              #print 'Making new bin for', item
>              bin = Bin()
>              bin.append(item)
>              bins.append(bin)
>
>      return bins
>
>
> if __name__ == '__main__':
>      import random
>
>      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'
>
>          bins = pack(aList, maxValue)
>
>          print 'Solution using', len(bins), 'bins:'
>          for bin in bins:
>              print bin
>
>          print
>
>
>      aList = [10,9,8,7,6,5,4,3,2,1]
>      packAndShow(aList, 11)
>
>      aList = [ random.randint(1, 11) for i in range(100) ]
>      packAndShow(aList, 11)


More information about the Tutor mailing list