Advertisement
iskhakovt

rebus

Apr 9th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import itertools
  2. import re
  3.  
  4. def main():
  5.     rebus = input()
  6.     letters = set()
  7.    
  8.     for ch in rebus:
  9.         if ch.isalpha():
  10.             letters.add(ch)
  11.            
  12.     if rebus.count('=') == 0 or not len(letters) or len(letters) > 10:
  13.         print('Bad rebus')
  14.         return
  15.     rebus = rebus.replace('=', '==')
  16.            
  17.     solutions = []
  18.     for perm in itertools.permutations([i for i in range(10)], len(letters)):
  19.         replace = {ch : str(perm[i]) for i, ch in enumerate(letters)}
  20.         pattern = re.compile('|'.join(replace.keys()))
  21.         curr = pattern.sub(lambda x: replace[x.group()], rebus)
  22.        
  23.         try:
  24.             if eval(curr):
  25.                 solutions.append(curr.replace('==', '='))
  26.         except:
  27.             pass
  28.    
  29.     if len(solutions):
  30.         print(len(solutions), 'solutions' if len(solutions) > 1 else 'solution', 'found:')
  31.     else:
  32.         print('no solutions found')
  33.     for sol in solutions:
  34.         print(sol)
  35.    
  36. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement