Last active
December 27, 2015 06:39
-
-
Save frydaykg/7283543 to your computer and use it in GitHub Desktop.
Knapsack problem solution #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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