def solve(s):
'''Find solutions to alphametic equations.
>>> solve('SEND + MORE == MONEY')
9567 + 1085 == 10652
'''
words = findall('[A-Za-z]+', s)
chars = set(''.join(words)) # characters to be substituted
assert len(chars) <= 10 # there are only ten possible digits
firsts = set(w[0] for w in words) # first letters of each of word
chars = ''.join(firsts) + ''.join(chars - firsts)
n = len(firsts) # chars[:n] cannot be assigned zero
for perm in permutations('0123456789', len(chars)):
if '0' not in perm[:n]:
trans = maketrans(chars, ''.join(perm))
equation = s.translate(trans)
try:
if eval(equation):
print equation
except ArithmeticError:
pass
评论列表
文章目录