Knapsack problem/Bounded: Difference between revisions

Content added Content deleted
(The previous Python non-zero-one solution is sensitive to the order of the elements in the items list and fails if the first item in the list is not included in the bagged items (e.g. if umbrella is first item))
(Made repaired python non-zero-one solution slightly more readable)
Line 3,428: Line 3,428:
v, lst = choose_item(400, len(items) - 1, {})
v, lst = choose_item(400, len(items) - 1, {})
w = 0
w = 0
for i, item in enumerate(lst):
for cnt, name in lst:
if item[0] > 0:
if cnt > 0:
print(*item)
print(cnt, name)
w = w + items[keys[keys.index(item[1])]][0] * item[0]
w = w + items[keys[keys.index(name)]][0] * cnt
print("Total weight:", w, "Value:", v)</lang>
print("Total weight:", w, "Value:", v)</lang>