Last active
December 30, 2015 18:09
-
-
Save hughdbrown/7866116 to your computer and use it in GitHub Desktop.
My brute force solution for knapsack problem, http://xkcd.com/287/
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
weights = [215, 275, 335, 355, 420, 580] | |
limit = 1505 | |
ds = { | |
i * w: [w] * i | |
for w in weights | |
for i in range(1, 1 + (limit // w)) | |
} | |
for w in weights: | |
for key, base in sorted(ds.items()): | |
for i in range(1, 1 + (limit - key) // w): | |
ds[key + w * i] = base + [w] * i | |
print sorted(ds.keys()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment