[ create a new paste ] login | about

Link: http://codepad.org/I1UIRF2q    [ raw code | output | fork ]

Python, pasted on Sep 9:
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])


Output:
1
2
3
bad
Total Value: 199
Total Weight: 101


Create a new paste based on this one


Comments: