Skip to content

Instantly share code, notes, and snippets.

@dnene
Created October 31, 2011 00:40
Show Gist options
  • Save dnene/1326654 to your computer and use it in GitHub Desktop.
Save dnene/1326654 to your computer and use it in GitHub Desktop.
Broken Weight problem - Challenge by @cbeust
# Earlier solution at : https://gist.github.com/1326624
# This one is the same logic, though a bit compact
# 4 weights are w1, w2, w3, w4 where w1 + w2 + w3 + w4 = 40
import itertools
n = 40
def listremove(list,val):
if val in list : list.remove(val)
return list
for weight in tuple((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 n-(w1+w2+w3) -w3 >= 0) :
allvals = list(range(1,n + 1))
for combo, other in tuple((combo,reduce(lambda l, val: listremove(l,val),combo,list(weight))) for clength in range(1,5) for combo in itertools.combinations(weight,clength)) :
allvals = reduce(lambda vals,val : listremove(allvals,abs(sum(combo) - sum(val))),tuple(o for olength in range(len(other) + 1) for o in itertools.combinations(other,olength)),allvals)
if len(allvals) == 0 : print weight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment