Skip to content

Commit 89de3ae

Browse files
authored
Create Filling Supermarket Bag
1 parent 65b429c commit 89de3ae

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Let’s say you’re in a supermarket.
2+
3+
You’ve found out that there is a huge discount on some items you want; unfortunately, you only have one bag with a certain bag_capacity. Each of the items has its own value and capacity, stored in lists called values and capacities contained in a dictionary called items.
4+
5+
Write a function to pick the items that maximize the total value without exceeding the bag’s capacity and return the total value.
6+
7+
Note: You can’t take more than one copy of each item home. Also, you do not need to fill the entire bag to capacity.
8+
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
9+
10+
def max_profit(items,bag_capacity):
11+
values = items['values']
12+
capacities = items['capacities']
13+
cur_value = 0
14+
res = 0
15+
cur_capacity = 0
16+
for ind, value in enumerate(capacities):
17+
if value < bag_capacity:
18+
cur_capacity = value
19+
cur_value+= values[ind]
20+
else:
21+
continue
22+
for j in range(ind + 1, len(capacities)):
23+
if cur_capacity + capacities[j] <= bag_capacity:
24+
res = max(res, cur_value + values[j])
25+
else:
26+
continue
27+
return res

0 commit comments

Comments
 (0)