Skip to content

Instantly share code, notes, and snippets.

@cmchap
Forked from jdherg/candy_checker.py
Last active August 29, 2015 14:18
Show Gist options
  • Save cmchap/6a5e94ef4bd8be9577d2 to your computer and use it in GitHub Desktop.
Save cmchap/6a5e94ef4bd8be9577d2 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))
for barnum in c:
if float(barnum).is_integer() == False:
print("Conflict at Machine %d. Candy bars are indivisible." % (barnum+1))
return
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