I'm working on a crypt-arithmetic solver to add two or more numbers. I've used brute-force to find all the permutations. I'm looking forward to improve this algorithm. Kindly review for all the aspects of this code and provide me your comments.
import string
import itertools
inListNumsAsStringArray = [ ['BASE', 'BALL'],
['SEND', 'MORE'] ]
inResultsArray = [ 'GAMES',
'MONEY' ]
inPossibleNumsAsStr = '0123456789'
# Input: (string, dictionary)
# string 'AB'
# key-value pairs for characters and numbers. Ex: dictCharAndDigit = {'A': '1', 'B': '2'}
# Output:(Number)
# 12
def getNumberFromStringAndMappingInfo(inStr, inDictMapping):
numAsStr = ''
for ch in inStr:
numAsStr = numAsStr + inDictMapping[ch]
return int(numAsStr)
def solveCryptarithmeticBruteForce(inListNumsAsString, inResultStr, inPossibleNumsAsStr):
nonZeroLetters = []
strFromStrList = ''
for numStr in inListNumsAsString:
nonZeroLetters.append(numStr[0])
strFromStrList = strFromStrList + numStr
nonZeroLetters.append(inResultStr[0])
strFromStrList = strFromStrList + inResultStr
uniqueStrs = ''.join(set(strFromStrList))
for tup in itertools.permutations(inPossibleNumsAsStr, len(uniqueStrs)):
dictCharAndDigit = {}
for i in range(len(uniqueStrs)):
dictCharAndDigit[uniqueStrs[i]] = tup[i]
nonZeroLetterIsZero = False
for letter in nonZeroLetters:
if(dictCharAndDigit[letter] == '0'):
nonZeroLetterIsZero = True
break
if(nonZeroLetterIsZero == True):
continue
result = getNumberFromStringAndMappingInfo(inResultStr, dictCharAndDigit)
testResult = 0
for numStr in inListNumsAsString:
testResult = testResult + getNumberFromStringAndMappingInfo(numStr, dictCharAndDigit)
if(testResult == result):
strToPrint = ''
for numStr in inListNumsAsString:
strToPrint = strToPrint + numStr + '(' + str(getNumberFromStringAndMappingInfo(numStr, dictCharAndDigit)) + ')' + ' + '
strToPrint = strToPrint[:-3]
strToPrint = strToPrint + ' = ' + inResultStr + '(' + str(result) + ')'
print(strToPrint)
break
for i in range(len(inResultsArray)):
solveCryptarithmeticBruteForce(inListNumsAsStringArray[i], inResultsArray[i], inPossibleNumsAsStr)