-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsackLight.py
More file actions
26 lines (23 loc) · 987 Bytes
/
knapsackLight.py
File metadata and controls
26 lines (23 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
You found two items in a treasure chest!
The first item weighs weight1 and is worth value1,
and the second item weighs weight2 and is worth value2.
What is the total maximum value of the items you can take with you,
assuming that your max weight capacity is maxW and you can't come back for the items later?
Note that there are only two items and you can't bring more than one item of each type,
i.e. you can't take two first items or two second items.
Example
For value1 = 10, weight1 = 5, value2 = 6, weight2 = 4, and maxW = 8, the output should be
knapsackLight(value1, weight1, value2, weight2, maxW) = 10.
"""
def knapsackLight(value1, weight1, value2, weight2, maxW):
if weight1 > maxW and weight2 > maxW:
return 0
elif weight1 + weight2 <= maxW:
return value1 + value2
elif weight1 <= maxW and weight2 <= maxW:
return max(value1, value2)
elif weight1 <= maxW and weight2 > maxW:
return value1
else:
return value2