Send More Money Puzzle in Python

cryptarithmetic program in python
each letter represents a different digit from 0 to 9
send + more = money c
cryptarithmetic solver
cryptarithmetic problem logic+logic=prolog
write a prolog program in a file named crypta pl to solve the cryptarithmetic puzzle which says
cryptarithmetic program in java
5.16 lab brute force equation solver python

A famous puzzle follows. SEND + MORE = MONEY

Substitute each letter in the equation with a single integer 0-9 (no duplicates) such that the addition is correct. Write a program to solve this puzzle. Hint: Brute force works well {try all possibilities}.

Here's my code so far:

def solution():
    letters = ('s', 'e', 'n', 'd', 'm', 'o', 'r', 'y')
    for s in range(9, 0, -1):
        for e in range(9, -1, -1):
            for n in range(9, -1, -1):
                for d in range(9, -1, -1):
                    for m in range(9, 0, -1):
                        for o in range(9, -1, -1):
                            for r in range(9, -1, -1):
                                for y in range(9, -1, -1):
                                    if len(set((letters))) != len(letters):
                                        send = 1000 * s + 100 * e + 10 * n + d
                                        more = 1000 * m + 100 * o + 10 * r + e
                                        money = 10000 * m + 1000 * o + 100 * n + 10 * e + y

                                        if send + more == money:
                                            return send, more, money

print(solution())

But it isn't working. It gives no output. How can I fix this?

with uniqueness requirement and search for all solutions:

def solutions():
    # letters = ('s', 'e', 'n', 'd', 'm', 'o', 'r', 'y')
    all_solutions = list()
    for s in range(9, -1, -1):
        for e in range(9, -1, -1):
            for n in range(9, -1, -1):
                for d in range(9, -1, -1):
                    for m in range(9, 0, -1):
                        for o in range(9, -1, -1):
                            for r in range(9, -1, -1):
                                for y in range(9, -1, -1):
                                    if len(set([s, e, n, d, m, o, r, y])) == 8:
                                        send = 1000 * s + 100 * e + 10 * n + d
                                        more = 1000 * m + 100 * o + 10 * r + e
                                        money = 10000 * m + 1000 * o + 100 * n + 10 * e + y

                                        if send + more == money:
                                            all_solutions.append((send, more, money))
    return all_solutions

print(solutions())

SEND + MORE = MONEY, Part 1, Introduction to Programming. SEND+MORE=MONEY. How can we get Python to solve puzzles like: SEND+MORE=MONEY. HAWAII + IDAHO + IOWA + OHIO =  If E = 6, then N = 7 which is not possible as D = 7. So we must have E = 5 and N = 6. This means D + E = 7 + 5 = 12, and thus Y = 2. So we have solved for all the letters! SEND + MORE = 9567 + 1085 = 10652.

I could not resist to write a little more concise brute-force solution since I never heard of the puzzle before.

from itertools import combinations, permutations

a, b, c = 'SEND', 'MORE', 'MONEY'

for comb in combinations(range(10), 8):
    for perm in permutations(comb):
        d = dict(zip('SENDMORY', perm))
        f = lambda x: sum(d[e] * 10**i for i, e in enumerate(x[::-1]))
        if f(a) + f(b) == f(c):
            print "{} + {} = {}".format(f(a), f(b), f(c))

Note that I did not exclude the possibility of S -> 0 or M -> 0 here.

EDIT: Similar, but using a generator and neglecting replacements that would lead to leading zeros.

from itertools import combinations, permutations

def replacements(): 
    for comb in combinations(range(10), 8): 
        for perm in permutations(comb): 
            if perm[0] * perm[1] != 0: 
                yield dict(zip('SMENDORY', perm))

a, b, c = 'SEND', 'MORE', 'MONEY'

for replacement in replacements(): 
    f = lambda x: sum(replacement[e] * 10**i for i, e in enumerate(x[::-1])) 
    if f(a) + f(b) == f(c):
        print('{} + {} = {}'.format(f(a), f(b), f(c)))

[PDF] Introduction to Programming SEND+MORE=MONEY, The following puzzle is probably the most well-known CryptArithmetic Problem: SEND MORE MONEY CryptArithmetic Problem and Solution. How to solve the  SEND + MORE = MONEY solved in 14 lines of Python. Close. 8. Posted by. u/scorpion032. 10 years ago. Archived. SEND + MORE = MONEY solved in 14 lines of Python

Casting lists to sets will only change their length if they have non-unique elements. Check how sets work: https://docs.python.org/2/library/sets.html

From community guidelines:

Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself. The first paragraph in your question is the second thing most readers will see, so make it as engaging and informative as possible.

SEND MORE MONEY CryptArithmetic Problem and Solution, "Math Puzzles Volume 2" is a sequel book with more great problems. Newsletter (sent Duration: 7:35 Posted: Sep 6, 2018 puzzles = ''' base + ball = games send + more = money ''' The advantages of this representation are that it is clear what it means (no need for complex documentation) and it keeps each puzzle on one line, so that adding and removing puzzles is easy.

@ siwica: your 'fast' solution provides a list of multiple results, so I propose a change in your second last line:

    if (f(a) + f(b) == f(c)) & (len(a) == len(str(f(a)))) & (len(b) == len(str(f(b)))):

so, I have no rights to comment, which is why I write this 'answer'

Only A "Genius" Can Solve - Each Letter Is A Number, Many have seen the familiar programming puzzle: SEND + MORE = MONEY. I This program takes such an equation, and generates a new Python. program  For instance, the famous cryptarithm SEND + MORE = MONEY is solved as M=1, Y=2, E=5, N=6, D=7, R=8, S=9 and O=0, giving 9567 + 1085 = 10652. Cryptarithms were invented by H. E. Dudeney in the July 1924 edition of Strand Magazine.

first we can get m = 1, use permutations to avoid same number appear

from itertools import permutations 

perm = permutations([0,2,3,4,5,6,7,8,9], 7)

for i in perm:

    if((i[0]*1000 + i[1]*100 + i[2]*10 + i[3] + 1000 + 100*i[4] + 10 * i[5] + i[1]) == (10000+1000*i[4]+i[2]*100+i[1]*10+i[6])):

        print(i[0]*1000 + i[1]*100 + i[2]*10 + i[3])
        print(1000 + 100*i[4] + 10 * i[5] + i[1])

Solve SEND + MORE = MONEY with Code Generation · GitHub, Javascript (ES6) implementation of a puzzle: each letter is a digit (0-9) and we have to find values for the letters in SEND MORE MONEY such that: 1. replacing​  Send More Money Puzzle in Python - Stack Overflow A famous puzzle follows. SEND + MORE = MONEY Substitute each letter in the equation with a single integer 0-9 (no duplicates) such that the addition is correct. Write a program to solve this puzz

SEND MORE MONEY · GitHub, The approach I would take would be to represent the puzzles like this: PUZZLES = ''' BASE + BALL = GAMES SEND + MORE = MONEY '''. SEND + MORE = MONEY is a classical ``crypto-arithmetic'' puzzle: the variables S, E, N, D, M, O, R, Y represent digits between 0 and 9, and the task is finding values for then such that the following arithmetic operation is correct:

Crypt-arithmetic solver (Brute force) in Python, SEND + MORE = MONEY is a classical ``crypto-arithmetic'' puzzle: the variables S, E, N, D, M, O, R, Y represent digits between 0 and 9, and the task is finding  "Math Puzzles Volume 2" is a sequel book with more great problems. (rated 4.5/5 stars on 6 reviews) Cryptarithmetic Problem with an Example SEND + MORE = MONEY - Duration: 13:08.

An Example: SEND + MORE = MONEY, Write a script (in Perl, Python, or some other language) that produces encoding of The SEND-MORE-MONEY puzzle is to assign to letters {D,E,M,N,O,R,  Newspapers and magazines often have crypt-arithmetic puzzles of the form: SEND + MORE ----- MONEY ----- The goal here is to assign each letter a digit from 0 to 9 so that the arithmetic works out correctly. The rules are that all occurrences of a letter must be assigned the same digit, and no digit can be assigned to more than one letter.

Comments
  • len(set((letters))) != len(letters) is invariant and never true. What is it for?
  • @Shankar That's not happening, though.
  • @Shankar - No such string multiplication is taking place.
  • I'm not sure what to use to meet the uniqueness requirement.
  • In keeping with the brute-force nature of your code, consider adding if e == s: continue at the top of the e loop, and something like if n in (s, e): continue in the n loop, and ... and if y in (s,e,n,d,m,o,r): continue at the top of the y loop.
  • This works great! How can I add a for loop at the end to return all the possible solutions for the problem?
  • It has multiple solutions since I assumed M -> 0 would be a valid replacement. The problem did not clearly exclude this option. If no leading zeros are allowed you would check for this case right at the beginning of the loop though and not at the point that you suggested. Also, I did not say my code was fast but only that it is concise. Those terms aren't always related.