Python 背包初级巨蟒

Python 背包初级巨蟒,python,recursion,knapsack-problem,Python,Recursion,Knapsack Problem,我很难找到适合初学者的背包问题的解决方案 def fill_bag(max_weight, value, weight): # Zero Weight if max_weight <= 0: return 0 # Invalid parameters if len(value) != len(weight): raise ValueError # Empty values elif value == [] or

我很难找到适合初学者的背包问题的解决方案

def fill_bag(max_weight, value, weight):
    # Zero Weight
    if max_weight <= 0:
        return 0
    # Invalid parameters
    if len(value) != len(weight):
        raise ValueError
    # Empty values
    elif value == [] or weight == []:
        return 0

    # Include current item in bag and recur for remaining items with decreased capacity
    include = value[-1] + fill_bag(max_weight - weight[-1], value[:-1], weight[:-1])

    # Exclude current item from bag and recur for remaining items
    exclude = fill_bag(max_weight, value[:-1], weight[:-1])

    return max(include, exclude)

print(fill_bag(10, [20, 5, 10, 40, 15, 25], [1, 2, 3, 8, 7, 4]))
def加注袋(最大重量、值、重量):
#零重量

如果最大重量,你可以固定压痕。如果这个袋子上有实际的测试空间,你可以在没有测试的情况下加入一个元素:打印(填充袋子(10、[1000,5,10,40,15,25],[1000,2,3,8,7,4])啊哈哈。我懂了。谢谢你,克里斯蒂安。你能修复压痕吗?如果这个袋子上有实际的测试空间,你可以在没有测试的情况下加入一个元素:打印(填充袋子(10、[1000,5,10,40,15,25],[1000,2,3,8,7,4])啊哈。我懂了。谢谢你,克里斯蒂安。