Please help understand. The capacity of the backpack is 6 (say, kg), you need to fit the maximum number of items into it, getting the most favorable result (max profit). I understand that 1 and 4 or 2 and 3 items should fall into the backpack.
from collections import namedtuple Good = namedtuple('Good', ['profit', 'weight']) goods = [Good(4, 5), Good(3, 4), Good(3, 2), Good(2, 1)] # sample data capacity = 6 def calc_profit(goods, capacity): s_goods = sorted(goods) items = capacity // s_goods return sum(goods[:items])