Skip to content

Commit

Permalink
knapsack
Browse files Browse the repository at this point in the history
  • Loading branch information
pronapro committed Aug 4, 2021
1 parent 0b0f2e5 commit 3435c0e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions knapsack.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 3435c0e

Please sign in to comment.