from itertools import takewhile
MAX_CAPACITY = 200
ITEMS = (
("bad", 101, 199),
("good", 100, 100),
("good", 100, 100)
)
def item_efficiency(item):
name, weight, value = item
return float(value)/float(weight)
def pack_bag(item):
name, weight, value = item
pack_bag.max_weight -= weight
return pack_bag.max_weight >= 0
pack_bag.max_weight = MAX_CAPACITY # static variable implementation
# pack the most efficient item until pack_bag.max_weight is reached.
pack = list(takewhile(pack_bag, reversed(sorted(ITEMS, key=item_efficiency))))
# print output
for item in pack:
print item[0]
table = zip(*pack)
print "Total Value: %i" % sum(table[2])
print "Total Weight: %i" % sum(table[1])