#Program 5 simple cryptarithm solver 3.4#11 #since we have not yet studied permu
ID: 3586427 • Letter: #
Question
#Program 5 simple cryptarithm solver 3.4#11 #since we have not yet studied permutation generators, twe will not write a general cryptarithm solver. Instead, let's write a special case: Given a puzzle using four digits, #such as a + b = cd, and given four specific numbers, such as 1, 14, 6, and 8, figure out which number goes for which digit to solve the cryptarithm. write the code for the solve function below. Test your code thoroughly before turning it in. 10 Given a cryptarithm puzzle using four letters, such as t['a', 'b',cd'), and a list of the four solution digits, such " tas [1, 4, 6,81, figure out the mapping of digits to letters that solves the cryptarithm. There will be two addends, so the length tof the puzzle list is 3. The solution should be expressed as a string: for example, 6+8-14' 15 " def solve (puzzle,digitList): YOUR CODE GOES HEREExplanation / Answer
Assumed puzzle:
[a,b,cd]
def solve(puzzle,digits):
size=len(digits)
concatenatedEle=[]
for i in range(0,size):
for j in range(0,size):
if(i!=j):
conc=(int)(str(digits[i])+str(digits[j]))
print(conc)
concatenatedEle.append(conc)
for i in range(0,size):
for j in range(i+1,size):
sum=digits[i]+digits[j]
if(sum in concatenatedEle):
print(str(digits[i])+'+'+str(digits[j])+'='+str(sum))
return
Explnation:
1. First nested loop finds all the concatenations. Sample output is as follows
14
16
18
41
46
48
61
64
68
81
84
86
2. Second for loop looks for puzzle match. i.e a+b=cd Once it finds such sample, it prints that sample and returns from the function.
You can return that statement also.
If you are looking more info, please let me know.
I corrected the space issues. You can use this link = https://repl.it/MMFb It has the code with proper indentation.
Let me make general solution.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.