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 |
for cnt, name in lst: |
||
if |
if cnt > 0: |
||
print( |
print(cnt, name) |
||
w = w + items[keys[keys.index( |
w = w + items[keys[keys.index(name)]][0] * cnt |
||
print("Total weight:", w, "Value:", v)</lang> |
print("Total weight:", w, "Value:", v)</lang> |