Skip to content

Instantly share code, notes, and snippets.

@frydaykg
Last active December 27, 2015 06:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frydaykg/7283543 to your computer and use it in GitHub Desktop.
Save frydaykg/7283543 to your computer and use it in GitHub Desktop.
Knapsack problem solution #1
def solve(k, a):
s = sum(a)
if s == k:
return True
elif s < k:
return False
elif len(a) == 1:
return False
else:
for i in range(len(a)):
temp = a[:i] + a[i+1:]
if solve(k, temp):
return True
return False
a = [2, 43, 6546457, 4, 5, 7, 34, 1]
k = 15
print solve(k, a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment