Skip to content

Commit aeee0f4

Browse files
Hardvancclauss
andauthored
Add doctests for fractional knapsack (TheAlgorithms#10891)
* Add doctests for fractional knapsack * Update greedy_methods/fractional_knapsack.py Co-authored-by: Christian Clauss <[email protected]> * Run doctests * Update greedy_methods/fractional_knapsack.py Co-authored-by: Christian Clauss <[email protected]> * Update greedy_methods/fractional_knapsack.py --------- Co-authored-by: Christian Clauss <[email protected]>
1 parent 28f4c16 commit aeee0f4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

greedy_methods/fractional_knapsack.py

+24
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ def frac_knapsack(vl, wt, w, n):
66
"""
77
>>> frac_knapsack([60, 100, 120], [10, 20, 30], 50, 3)
88
240.0
9+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 10, 4)
10+
105.0
11+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 8, 4)
12+
95.0
13+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6], 8, 4)
14+
60.0
15+
>>> frac_knapsack([10, 40, 30], [5, 4, 6, 3], 8, 4)
16+
60.0
17+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 0, 4)
18+
0
19+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 8, 0)
20+
95.0
21+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], -8, 4)
22+
0
23+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 8, -4)
24+
95.0
25+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 800, 4)
26+
130
27+
>>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 8, 400)
28+
95.0
29+
>>> frac_knapsack("ABCD", [5, 4, 6, 3], 8, 400)
30+
Traceback (most recent call last):
31+
...
32+
TypeError: unsupported operand type(s) for /: 'str' and 'int'
933
"""
1034

1135
r = sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True)

0 commit comments

Comments
 (0)