Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- def solve_mathagram(mathagram, n):
- l = list(range(1, 10)) * n
- for i in list(mathagram):
- if i.isdigit():
- l.remove(int(i))
- m = mathagram.replace('x', '{}')
- for g in permutations(l, mathagram.count('x')):
- x = m.format(*g).split('=')
- s1, s2 = map(lambda a: sum(map(int, a.split("+"))), x)
- if s1 == s2:
- return "=".join(x).strip()
- import profile
- def run():
- for i in ['xxx + xxx + xxx + 4x1 + 689 = xxx + xxx + x5x + 957',
- 'xxx + xxx + xxx + 64x + 581 = xxx + xxx + xx2 + 623',
- 'xxx + xxx + xxx + x81 + 759 = xxx + xxx + 8xx + 462',
- 'xxx + xxx + xxx + 6x3 + 299 = xxx + xxx + x8x + 423',
- 'xxx + xxx + xxx + 58x + 561 = xxx + xxx + xx7 + 993']:
- solve_mathagram(i, 3)
- profile.run('run()')
Add Comment
Please, Sign In to add comment