Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Last active December 30, 2015 18:09
Show Gist options
  • Save hughdbrown/7866116 to your computer and use it in GitHub Desktop.
Save hughdbrown/7866116 to your computer and use it in GitHub Desktop.
My brute force solution for knapsack problem, http://xkcd.com/287/
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