-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryto_arthrmatic.py
33 lines (24 loc) · 1.02 KB
/
cryto_arthrmatic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from itertools import permutations
def solve_cryptarithmetic(puzzle):
letters = set("".join(puzzle))
print(letters)
if len(letters) > 10:
return "Too many letters for unique digit assignment."
for perm in permutations(range(10), len(letters)):
if all(perm[puzzle[i].index(puzzle[i][0])] != 0 for i in range(len(puzzle))):
mapping = dict(zip(letters, perm))
num1 = int("".join([str(mapping[c]) for c in puzzle[0]]))
num2 = int("".join([str(mapping[c]) for c in puzzle[1]]))
num3 = int("".join([str(mapping[c]) for c in puzzle[2]]))
if num1 + num2 == num3:
return mapping
return "No solution found."
def main():
print("Example : \n First word + Second word = Third word")
a = input("ENter the first word = ")
b = input("ENter the second word = ")
c = input("ENter the third word = ")
puzzle = [a, b, c]
solution = solve_cryptarithmetic(puzzle)
print(solution)
main()