Interview Query

Filling Supermarket Bag

Have you seen this question before?

Let’s say you’re in a supermarket.

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.

Write a function to pick the items that maximize the total value without exceeding the bag’s capacity and return the total value.

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.



items = {
  "capacities":[2, 4, 6, 5]

bag_capacity = 8


max_profit(items,bag_capacity) -> 6

We choose the first and last item with a total profit of 6 and a capacity of 7, which is less than the bag_capacity.

Next question: Google Docs Drop
Python 3.9.6
Loading editor
Use Shift + Enter to run code