Guest User

Untitled

a guest
Oct 13th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. from itertools import permutations
  2. def solve_mathagram(mathagram, n):
  3. l = list(range(1, 10)) * n
  4. for i in list(mathagram):
  5. if i.isdigit():
  6. l.remove(int(i))
  7. m = mathagram.replace('x', '{}')
  8. for g in permutations(l, mathagram.count('x')):
  9. x = m.format(*g).split('=')
  10. s1, s2 = map(lambda a: sum(map(int, a.split("+"))), x)
  11. if s1 == s2:
  12. return "=".join(x).strip()
  13.  
  14. import profile
  15. def run():
  16. for i in ['xxx + xxx + xxx + 4x1 + 689 = xxx + xxx + x5x + 957',
  17. 'xxx + xxx + xxx + 64x + 581 = xxx + xxx + xx2 + 623',
  18. 'xxx + xxx + xxx + x81 + 759 = xxx + xxx + 8xx + 462',
  19. 'xxx + xxx + xxx + 6x3 + 299 = xxx + xxx + x8x + 423',
  20. 'xxx + xxx + xxx + 58x + 561 = xxx + xxx + xx7 + 993']:
  21. solve_mathagram(i, 3)
  22. profile.run('run()')
Add Comment
Please, Sign In to add comment