mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Adding doctests for fractional_knapsack.py (#1331)
* Adding doctests for fractional_knapsack.py * Update fractional_knapsack.py
This commit is contained in:
parent
9c634735d3
commit
ddb094919b
|
@ -3,6 +3,10 @@ from bisect import bisect
|
|||
|
||||
|
||||
def fracKnapsack(vl, wt, W, n):
|
||||
"""
|
||||
>>> fracKnapsack([60, 100, 120], [10, 20, 30], 50, 3)
|
||||
240.0
|
||||
"""
|
||||
|
||||
r = list(sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True))
|
||||
vl, wt = [i[0] for i in r], [i[1] for i in r]
|
||||
|
@ -16,5 +20,7 @@ def fracKnapsack(vl, wt, W, n):
|
|||
else sum(vl[:k])
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
print("%.0f" % fracKnapsack([60, 100, 120], [10, 20, 30], 50, 3))
|
||||
doctest.testmod()
|
||||
|
|
Loading…
Reference in New Issue
Block a user