import sys BIN_SIZE = 145#1.0005 # unnecessary concern about precision errors? # load in the items items = [] for line in open(sys.argv[1]): items.append(float(line)) # sort them items.sort() bins = [] while len(items) > 0: x = items.pop() placed = False # i is an index, b in the ith element for i, b in enumerate(bins): if b+x <= BIN_SIZE: bins[i] = b+x placed = True # if it doesn't fit in a bin, make a new one with it in it if not placed: bins.append(x) # note: easy to figure out exact answer. print len(bins), bins