Skip to content

Commit ecc89b5

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 836a8d0 commit ecc89b5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

knapsack/recursive_approach_knapsack.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616

1717
from functools import lru_cache
1818

19-
def knapsack(weights: list, values: list, number_of_items: int, max_weight: int, index: int, memo=None) -> int:
19+
20+
def knapsack(
21+
weights: list,
22+
values: list,
23+
number_of_items: int,
24+
max_weight: int,
25+
index: int,
26+
memo=None,
27+
) -> int:
2028
"""
2129
Optimized Recursive Knapsack with Memoization.
2230
@@ -49,7 +57,14 @@ def knapsack(weights: list, values: list, number_of_items: int, max_weight: int,
4957
# Case 2: Include the current item (if weight allows)
5058
ans2 = 0
5159
if weights[index] <= max_weight:
52-
ans2 = values[index] + knapsack(weights, values, number_of_items, max_weight - weights[index], index + 1, memo)
60+
ans2 = values[index] + knapsack(
61+
weights,
62+
values,
63+
number_of_items,
64+
max_weight - weights[index],
65+
index + 1,
66+
memo,
67+
)
5368

5469
# Store and return the maximum value obtained
5570
memo[(index, max_weight)] = max(ans1, ans2)
@@ -58,4 +73,5 @@ def knapsack(weights: list, values: list, number_of_items: int, max_weight: int,
5873

5974
if __name__ == "__main__":
6075
import doctest
76+
6177
doctest.testmod()

0 commit comments

Comments
 (0)