Created
May 28, 2010 13:35
-
-
Save nb/417149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
E=7 | |
TALLET + | |
SIGNAL | |
------ | |
ELAINE | |
""" | |
letters = set('T A L L E T S I G N A L E L A I N E'.split()) | |
assoc = {letter: None for letter in letters} | |
assoc['E'] = 7 | |
def number_from_letters(letters, assoc): | |
return sum(10**power*assoc[letter] for (power, letter) in enumerate(reversed(letters))) | |
def ok(assoc): | |
return len(set(assoc.values())) == len(assoc) and (number_from_letters('TALLET', assoc) + number_from_letters('SIGNAL', assoc) == number_from_letters('ELAINE', assoc)) | |
def solve(assoc): | |
available_digits = reversed(tuple(set(range(0, 10)) - set(assoc.values()))) | |
for letter in assoc: | |
if assoc[letter] is None: | |
for digit in available_digits: | |
assoc[letter] = digit | |
if solve(assoc): | |
return assoc | |
assoc[letter] = None | |
return assoc if (not None in assoc.values()) and ok(assoc) else False | |
if __name__ == '__main__': | |
result = solve(assoc) | |
print(result) | |
print(number_from_letters('TALLET', assoc)) | |
print(number_from_letters('SIGNAL', assoc)) | |
print(number_from_letters('ELAINE', assoc)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment