Cryptarithmetic Puzzlewith Python-Constraint: A Creative Approach to Constraint Satisfaction
The $python$-$constraint$ library is a powerful tool for solving constraint satisfaction problems (CSPs) in $Python$.
It allows you to define variables, domains, and constraints, then finds solutions that satisfy all the constraints.
While the basic usage is straightforward, here are some unconventional or “curious” ways to use $python$-$constraint$ that showcase its flexibility.
Cryptarithmetic Puzzle
$Cryptarithmetic$ $puzzles$ involve solving mathematical equations where digits are replaced with letters.
For example, in the puzzle SEND + MORE = MONEY, each letter represents a unique digit.
1 | from constraint import Problem, AllDifferentConstraint |
Explanation
This $Python$ code uses the python-constraint library to solve a $cryptarithmetic$ $puzzle$, specifically the puzzle SEND + MORE = MONEY.
In this type of puzzle, each letter represents a unique digit, and the goal is to find the digits that satisfy the equation.
Step-by-Step Explanation:
Importing the Required Modules:
1
from constraint import Problem, AllDifferentConstraint
The
Problemclass from thepython-constraintlibrary is used to define and solve constraint satisfaction problems (CSPs).
TheAllDifferentConstraintensures that all variables (letters) represent different digits.Creating the Problem Instance:
1
problem = Problem()
A
Problemobject is instantiated, which will be used to define the variables and constraints for this puzzle.Defining Variables:
1
2letters = 'SENDMORY'
problem.addVariables(letters, range(10))The letters in the puzzle (‘S’, ‘E’, ‘N’, ‘D’, ‘M’, ‘O’, ‘R’, ‘Y’) are treated as variables.
Each letter can take a value between $0$ and $9$, representing a digit.Adding the All-Different Constraint:
1
problem.addConstraint(AllDifferentConstraint(), letters)
This constraint ensures that all letters represent different digits.
For example, ‘S’ cannot be the same as ‘E’, and so on.Adding Leading Digit Constraints:
1
2problem.addConstraint(lambda S: S != 0, 'S')
problem.addConstraint(lambda M: M != 0, 'M')These constraints ensure that the leading digits of the numbers formed by the letters cannot be zero.
For example, ‘S’ (the first digit of “SEND”) and ‘M’ (the first digit of “MORE” and “MONEY”) must be non-zero.Defining the Equation Constraint:
1
2
3
4
5def equation(S, E, N, D, M, O, R, Y):
send = S * 1000 + E * 100 + N * 10 + D
more = M * 1000 + O * 100 + R * 10 + E
money = M * 10000 + O * 1000 + N * 100 + E * 10 + Y
return send + more == moneyThis function converts the letters into the corresponding numbers and defines the equation
SEND + MORE = MONEY.
It calculates the numeric values of “SEND”, “MORE”, and “MONEY” and checks if the sum of “SEND” and “MORE” equals “MONEY”.Adding the Equation Constraint to the Problem:
1
problem.addConstraint(equation, 'SENDMORY')
The
equationfunction is added as a constraint to the problem, ensuring that the solution must satisfy the equationSEND + MORE = MONEY.Solving the Problem:
1
solution = problem.getSolution()
The
getSolution()method attempts to find a solution that satisfies all the constraints.
If a solution exists, it returns the solution as a dictionary mapping each letter to its corresponding digit.Printing the Solution:
1
2
3
4if solution:
print(solution)
else:
print("No solution found.")If a solution is found, it is printed. Otherwise, the program outputs “No solution found.”
Summary:
This code solves the $cryptarithmetic$ $puzzle$ SEND + MORE = MONEY using constraint satisfaction techniques.
It assigns digits to each letter while ensuring that the digits satisfy the puzzle’s rules:
no two letters have the same digit, the first letters (‘S’ and ‘M’) are non-zero, and the equation SEND + MORE = MONEY holds true.
Output
[Output]
1 | {'M': 1, 'S': 9, 'D': 7, 'E': 5, 'N': 6, 'O': 0, 'R': 8, 'Y': 2} |
The result {'M': 1, 'S': 9, 'D': 7, 'E': 5, 'N': 6, 'O': 0, 'R': 8, 'Y': 2} represents a valid solution to the $cryptarithmetic$ $puzzle$ SEND + MORE = MONEY.
Explanation of the Solution:
- Letter-to-Digit Mapping:
M = 1S = 9D = 7E = 5N = 6O = 0R = 8Y = 2
Substitution in the Equation:
- SEND becomes
9567(S = 9,E = 5,N = 6,D = 7) - MORE becomes
1085(M = 1,O = 0,R = 8,E = 5) - MONEY becomes
10652(M = 1,O = 0,N = 6,E = 5,Y = 2)
Verification:
The equation SEND + MORE = MONEY can be verified as follows:
9567 + 1085 = 10652- The sum is correct, and the equation holds true.
Conclusion:
This mapping of letters to digits correctly satisfies the $cryptarithmetic$ $puzzle$, making it a valid solution.
The unique digits assigned to each letter, along with the correct calculation, ensure that the equation SEND + MORE = MONEY is fulfilled.


















