knapsack.knapsack¶
A naive recursive implementation of 0-1 Knapsack Problem https://en.wikipedia.org/wiki/Knapsack_problem
Functions¶
|
Returns the maximum value that can be put in a knapsack of a capacity cap, |
Module Contents¶
- knapsack.knapsack.knapsack(capacity: int, weights: list[int], values: list[int], counter: int) int ¶
Returns the maximum value that can be put in a knapsack of a capacity cap, whereby each weight w has a specific value val.
>>> cap = 50 >>> val = [60, 100, 120] >>> w = [10, 20, 30] >>> c = len(val) >>> knapsack(cap, w, val, c) 220
The result is 220 cause the values of 100 and 120 got the weight of 50 which is the limit of the capacity.