Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Good morning! Here's your coding interview problem for today.
- This problem was asked by Google.
- A cryptarithmetic puzzle is a mathematical game where the digits of some numbers are represented by letters.
- Each letter represents a unique digit.
- For example, a puzzle of the form:
- SEND
- + MORE
- --------
- MONEY
- may have the solution:
- {'S': 9, 'E': 5, 'N': 6, 'D': 7, 'M': 1, 'O', 0, 'R': 8, 'Y': 2}
- Given a three-word puzzle like the one above, create an algorithm that finds a solution.
- '''
- from itertools import permutations
- from timeit import default_timer as timer
- # FUNCTION 1
- def breakMessage(message):
- # First, I have to find the '+' and '=' characters
- plusIndex = message.index('+')
- equalsIndex = message.index('=')
- word1 = message[0 : plusIndex-1]
- word2 = message[plusIndex+2 : equalsIndex-1]
- word3 = message[equalsIndex+2:]
- return word1, word2, word3
- # FUNCTION 2
- def uniqueLetters(word1, word2, word3):
- words = word1 + word2 + word3
- unique = list()
- for letter in words:
- isUnique = True
- for un in unique:
- if letter == un:
- isUnique = False
- break
- if isUnique == True:
- unique.append(letter)
- return unique
- # FUNCTION 3
- def matchLists(list1, list2):
- if len(list1) != len(list2):
- print("Error while using function " + matchLists.__name__)
- dictionary = dict()
- for i in range(len(list1)):
- dictionary[list1[i]] = list2[i]
- return dictionary
- # FUNCTION 4
- def match(message):
- word1, word2, word3 = breakMessage(message)
- unique = uniqueLetters(word1, word2, word3)
- # Now, we have a list with unique letters. Let's suppose N unique letters
- # Each unique letter can take values from 0 to 9, so there are
- # 10 ^ N possible arrangements. So I will create all the 10^N possible
- # permutations. Each index of permutation can be from 0 to 9.
- # I will equalise each permutation with the letters
- N = len(unique)
- perms = permutations(list(range(10)), N)
- N1 = len(word1)
- N2 = len(word2)
- N3 = len(word3)
- solutionLetters = dict()
- solutionNumbers = list()
- for perm in perms:
- dictionary = matchLists(unique, perm)
- # print(dictionary)
- number1 = 0
- number2 = 0
- number3 = 0
- # ===============================================================================
- # ===============================================================================
- # Word 1 ----> Number 1
- for i in range(N1):
- letter = word1[i]
- digit = -1
- for key in dictionary:
- if letter == key:
- digit = dictionary[key]
- break
- number1 += digit * (10**(N1-1-i))
- # Word 2 ----> Number 2
- for i in range(N2):
- letter = word2[i]
- digit = -1
- for key in dictionary:
- if letter == key:
- digit = dictionary[key]
- break
- number2 += digit * (10**(N2-1-i))
- # Word 3 ----> Number 3
- for i in range(N3):
- letter = word3[i]
- digit = -1
- for key in dictionary:
- if letter == key:
- digit = dictionary[key]
- break
- number3 += digit * (10**(N3-1-i))
- # ===============================================================================
- # ===============================================================================
- # Now, I have all the 3 numbers I want
- perm = [number1, number2, number3]
- # print(perm)
- if number1 + number2 == number3:
- # print("YES, WE FOUND A SOLUTION TO THIS PROBLEM")
- solutionLetters = dictionary
- solutionNumbers = perm
- break
- if len(solutionLetters) != 0:
- # We have found a solution
- return solutionLetters, solutionNumbers
- else:
- # There is no solution
- return {}, []
- # FUNCTION 5
- def prettyPrint(message):
- start = timer()
- print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
- solutionLetters, solutionNumbers = match(message)
- print(message)
- if len(solutionLetters) != 0:
- result = ""
- M = len(solutionLetters)
- counter = 0
- for key in solutionLetters:
- if counter != M - 1:
- result += key + " = " + str(solutionLetters[key]) + ", "
- else:
- result += key + " = " + str(solutionLetters[key])
- print("Solution for: " + result)
- print(str(solutionNumbers[0]) + " + " + str(solutionNumbers[1]) + " = " + str(solutionNumbers[2]))
- end = timer()
- elapsed = end - start
- print()
- print("Execution time: " + str(round(elapsed, 4)) + " seconds.")
- print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
- print()
- else:
- print("There is no solution for this problem")
- print("Next time")
- # MAIN FUNCTION
- # RULES
- print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
- print("We are trying to match letters with numbers, so we can have an acceptable addition")
- print("But, the user has to leave blank space between and after the symbols '+' and '='")
- print("One good example is: send + more = money")
- print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
- print()
- message1 = "ab + ba = cc"
- prettyPrint(message1)
- message2 = "send + more = money"
- prettyPrint(message2)
- message3 = input("Message: ")
- prettyPrint(message3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement