Created
November 2, 2013 22:00
-
-
Save frydaykg/7283974 to your computer and use it in GitHub Desktop.
Knapsack problem #3
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): | |
b = set() | |
b.add(0) | |
b.add(a[0]) | |
for i in range(1, len(a)): | |
tmp = set() | |
for j in b: | |
tmp.add(j) | |
if j + a[i] <= k: | |
tmp.add(j + a[i]) | |
b = tmp | |
return k in b | |
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