Skip to content

Instantly share code, notes, and snippets.

@jdherg
Created April 3, 2015 22:51
Show Gist options
  • Save jdherg/b3e1ba14b9c04c66bf9b to your computer and use it in GitHub Desktop.
Save jdherg/b3e1ba14b9c04c66bf9b to your computer and use it in GitHub Desktop.
import sys
def calc_weight(c, m):
return sum(
[c_i * (m_i + 100) for m_i, c_i in zip(m, c)])
def main():
if len(sys.argv) < 1:
print("Usage: python candy_checker.py n_i_expression")
c_func = eval("lambda n: " + sys.argv[1])
c = list(map(c_func, range(1, 11)))
print(c)
print("%d candybars" % sum(c))
weights = dict()
for i in range(1023):
str_rep = bin(i)[2:]
str_rep = (10 - len(str_rep)) * "0" + str_rep
m = list(map(int, str_rep))
m.reverse()
weight = calc_weight(c, m)
if weight in weights:
print("Conflict at %d" % weight)
print(weights[weight])
print(calc_weight(c, weights[weight]))
print(m)
print(calc_weight(c, m))
return
weights[weight] = m
print("Passed")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment