Created
October 31, 2011 17:40
-
-
Save mlbright/1328114 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
# Response to challenge at http://beust.com/weblog/2011/10/30/a-new-coding-challenge/ | |
import sys | |
from itertools import combinations, chain | |
N = 40 | |
def powerset(iterable): | |
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" | |
s = list(iterable) | |
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) | |
weight_combos = ([w1,w2,w3,N-(w1+w2+w3)] \ | |
for w1 in range(1,N) \ | |
for w2 in range(w1,N-w1) \ | |
for w3 in range(w2,N-w1-w2) if w1+w2+w3+w3 <= 40) | |
for weights in weight_combos: | |
vals = set(range(1,N+1)) | |
for left in powerset(weights): | |
for right in powerset([w for w in weights if w not in left]): | |
diff = abs(sum(left) - sum(right)) | |
vals.discard(diff) | |
if len(vals) == 0: | |
print weights | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment