Open the context
Choose the project folder and name the files or behavior involved.
Maximize value from at most 64 weight-value pairs with an integer capacity from zero through 1000, using each item once. Test zero capacity and an oversized item, rejecting zero weights or non-integer operands before building the table.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
def knapsack_value(items, capacity):
items = tuple(tuple(item) for item in items)
if type(capacity) is not int or not 0 <= capacity <= 1000:
raise ValueError("capacity.range")
if len(items) > 64 or any(len(item) != 2 for item in items):
raise ValueError("items.shape")
if any(type(w) is not int or type(v) is not int or not 1 <= w <= 1000 or not 0 <= v <= 1000000 for w, v in items):
raise ValueError("item.range")
best = [0] * (capacity + 1)
for weight, value in items:
for limit in range(capacity, weight - 1, -1):
best[limit] = max(best[limit], best[limit - weight] + value)
return best[capacity]The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.