Skip to content

Commit aa93e55

Browse files
committed
Add strcit=True to zip function
1 parent 06edbaf commit aa93e55

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

greedy_methods/fractional_knapsack.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ def frac_knapsack(
2626
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 8, 4)
2727
95.0
2828
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6], 8, 4)
29-
60.0
29+
Traceback (most recent call last):
30+
...
31+
ValueError: zip() argument 2 is shorter than argument 1
3032
>>> frac_knapsack([10, 40, 30], [5, 4, 6, 3], 8, 4)
31-
60.0
33+
Traceback (most recent call last):
34+
...
35+
ValueError: zip() argument 2 is longer than argument 1
3236
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 0, 4)
3337
0
3438
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 8, 0)
@@ -48,7 +52,7 @@ def frac_knapsack(
4852
"""
4953

5054
# sort in descending order of value/weight ratio
51-
r = sorted(zip(values, weights),
55+
r = sorted(zip(values, weights, strict=True),
5256
key=lambda x: x[0] / x[1], reverse=True)
5357

5458
values, weights = [i[0] for i in r], [i[1] for i in r] # unzip the list

0 commit comments

Comments
 (0)