Created
January 13, 2017 00:02
-
-
Save Radcliffe/04bb0c62069083b0e2e6956ea9c9a3a9 to your computer and use it in GitHub Desktop.
Python script to solve additive cryptarithms
This file contains hidden or 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
#!/usr/bin/env python | |
# This programs solves additive cryptarithms using brute force. | |
# Example: solve_cryptarithm(['SEND', 'MORE', 'MONEY']) returns a list of | |
# all solutions to the equation SEND + MORE = MONEY, where each letter | |
# stands for a different digit in base 10. Leading zeros are not allowed. | |
from itertools import permutations | |
import sys | |
def word_to_number(word, lookup): | |
"""Convert a word to a number by replacing each letter with a digit, using a lookup dictionary.""" | |
return int(''.join(str(lookup[letter]) for letter in word)) | |
def solve_cryptarithm(words): | |
"""Solve an equation like SEND + MORE = MONEY given a list of three or more words. | |
The last word should be the sum of the other words.""" | |
words = [word.upper() for word in words] | |
letters = list(set(''.join(words))) | |
if len(letters) > 10: | |
print('Too many distinct letters (%d).' % len(letters)) | |
return | |
for perm in permutations(range(10), len(letters)): | |
lookup = dict(zip(letters, perm)) | |
if all(lookup[w[0]] > 0 for w in words): | |
numbers = [word_to_number(w, lookup) for w in words] | |
if sum(numbers[:-1]) == numbers[-1]: | |
yield numbers | |
if __name__ == '__main__': | |
if len(sys.argv) < 4: | |
print('At least three arguments required.') | |
else: | |
for solution in solve_cryptarithm(sys.argv[1:]): | |
print (' + '.join(str(n) for n in solution[:-1]) + ' = ' + str(solution[-1])) |
TonyHoldroyd
commented
Sep 11, 2021
via email
Caio David
That's very good of you, it's a lovely, neat, compact solution that would
have taken me all week to come up with.
And it works pretty quickly on my aging Ubuntu box.
I'll give you proper attribution on any use that I make of it.
Thanks again.
Best,
Tony
On Sat, Sep 11, 2021 at 12:27 PM David Radcliffe ***@***.***>
wrote:
… ***@***.**** commented on this gist.
------------------------------
Hi Tony,
You are welcome to use my code.
On Sat, Sep 11, 2021, 5:00 AM Tony Holdroyd ***@***.***>
wrote:
> ***@***.**** commented on this gist.
> ------------------------------
>
> Ciao David,
> I'm writing to ask your permission to use you code here as the basis for
> some other work I'm doing in a Q# project to solve the cryptarithm
problem
> qith QC.
> Thank you,
> Best
> Tony Holdroyd ***@***.***)
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <
https://gist.github.com/04bb0c62069083b0e2e6956ea9c9a3a9#gistcomment-3889560
>,
> or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/AAFFDJFISDYPXCGAT3VQEE3UBMSE5ANCNFSM5D2ZP5WA
>
> .
> Triage notifications on the go with GitHub Mobile for iOS
> <
https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675
>
> or Android
> <
https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub
>.
>
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/04bb0c62069083b0e2e6956ea9c9a3a9#gistcomment-3889569>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACS4UW6O64DYVIZN6XIXYULUBMVJ3ANCNFSM5D2ZP5WA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment