Adding doctests for fractional_knapsack.py (#1331)

* Adding doctests for fractional_knapsack.py

* Update fractional_knapsack.py
This commit is contained in:
Jigyasa G 2019-10-18 11:43:20 +05:30 committed by Christian Clauss
parent 9c634735d3
commit ddb094919b

View File

@ -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()