Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- import re
- def main():
- rebus = input()
- letters = set()
- for ch in rebus:
- if ch.isalpha():
- letters.add(ch)
- if rebus.count('=') == 0 or not len(letters) or len(letters) > 10:
- print('Bad rebus')
- return
- rebus = rebus.replace('=', '==')
- solutions = []
- for perm in itertools.permutations([i for i in range(10)], len(letters)):
- replace = {ch : str(perm[i]) for i, ch in enumerate(letters)}
- pattern = re.compile('|'.join(replace.keys()))
- curr = pattern.sub(lambda x: replace[x.group()], rebus)
- try:
- if eval(curr):
- solutions.append(curr.replace('==', '='))
- except:
- pass
- if len(solutions):
- print(len(solutions), 'solutions' if len(solutions) > 1 else 'solution', 'found:')
- else:
- print('no solutions found')
- for sol in solutions:
- print(sol)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement