From 3435c0ed0c02e08494dd7801276d16865497bed4 Mon Sep 17 00:00:00 2001 From: pronapro Date: Wed, 4 Aug 2021 21:17:18 +0300 Subject: [PATCH] knapsack --- knapsack.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 knapsack.py diff --git a/knapsack.py b/knapsack.py new file mode 100644 index 0000000..c1b5288 --- /dev/null +++ b/knapsack.py @@ -0,0 +1,13 @@ +def knapsack(capacity,n,size,value): + if n==0 or capacity ==0: + return 0 + elif size[n-1]>capacity: + return knapsack(capacity,n-1,size,value) + else: + return max(value[n-1]+knapsack(capacity-size[n-1],n-1,size,value),knapsack(capacity,n-1,size,value)) + +capacity = 16 +n = 5 +size = [3,4,7,8,9] +value =[4,5,10,11,13] +print(knapsack(capacity,n,size,value)) \ No newline at end of file