Question

Given a cryptarithm puzzle using four letters, such as ['a','b','cd'], and a list of the four...

Given a cryptarithm puzzle using four letters, such as ['a','b','cd'], and a list of the four solution digits, such as [1,4,6,8], figure out the mapping of digits to letters that solves the cryptarithm. There will be two addends, so the length of the puzzle list is 3. The solution should be expressed as a

string; for example, '6+8=14

This is the algorithm to follow

            Create a list of the four distinct letters used in the puzzle. Example: ['a','b','c','d']

            For each number in the solution digit list,

                        Let the first letter from the letter list be associated with that number

                        For each remaining number in the solution digit list,

                                    Let the second letter in the letter list be associated with that number

                                    For each remaining number in the solution digit list,

                                                Let the third letter be associated with this number

                                                Let the fourth letter be associated with the other number.

                                                Check to see if these number associations solve the puzzle or not

                                                If so, return the solution in the required format

            If no solutions are found, return a helpful message.

Type the possible code.

def solve(puzzle,digitList):

.

Homework Answers

Answer #1

Please find the below code for the above mentioned assignment, I hope this will help you if yes, give me a thumps up!

def solve(puzzle, digitList):
    distinct_letters = []
    letter_association = {}
    solution = []
    for i in puzzle:
        for x in i:
            if x not in distinct_letters:
                distinct_letters.append(x)
                letter_association[x] = ""
    for i in digitList:
        letter_association[distinct_letters[0]] = digitList[digitList.index(i)]
        for j in (list(list(set(digitList) - {i}))):
            letter_association[distinct_letters[1]] = digitList[digitList.index(j)]
            for k in (list(list(set(digitList) - {i, j}))):
                letter_association[distinct_letters[2]] = digitList[digitList.index(k)]
                for l in (list(list(set(digitList) - {i, j, k}))):
                    letter_association[distinct_letters[3]] = digitList[digitList.index(l)]
                    expr_1 = puzzle[0]
                    expr_2 = puzzle[1]
                    expr_3 = puzzle[2]
                    int_1 = ""
                    int_2 = ""
                    int_3 = ""
                    for z in expr_1:
                        int_1 += str(letter_association[z])
                    int_1 = int(int_1)
                    for z in expr_2:
                        int_2 += str(letter_association[z])
                    int_2 = int(int_2)
                    for z in expr_3:
                        int_3 += str(letter_association[z])
                    int_3 = int(int_3)
                    if int_1 + int_2 == int_3:
                        solution.append("{}+{}={}".format(int_1, int_2, int_3))
    if len(solution) == 0:
        print("No Solutions found for the puzzle!!")
        return [-1]
    return solution


def testCase(testNumber, puzzle, digits, expectedList):
    actualResult = solve(puzzle, digits)
    if sorted(actualResult) == sorted(expectedList):
        print("Test ", testNumber, " passed.")
    else:
        print("Test ", testNumber, " failed. Expected one of: {} Actual: {}".format(expectedList, actualResult))


def test():
    testCase(1, ['a', 'b', 'cd'], [1, 4, 6, 8], ['6+8=14', '8+6=14'])
    testCase(2, ['q', 'r', 'st'], [5, 2, 1, 7], ['5+7=12', '7+5=12'])
    testCase(3, ['xyz', 'zx', 'xzw'], [4, 7, 0, 3], ['403+34=437', '304+43=347'])

test()
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I'm trying to write a solver that can find a single solution to a cryptarithmetic puzzle....
I'm trying to write a solver that can find a single solution to a cryptarithmetic puzzle. I need help writing the other function from this code. /* ExhaustiveSolve * --------------- * This is the "not-very-smart" version of cryptarithmetic solver. It takes * the puzzle itself (with the 3 strings for the two addends and sum) and a * string of letters as yet unassigned. If no more letters to assign * then we've hit a base-case, if the current letter-to-digit...
Four different letters are distributed at random into 5 mailboxes by choosing one of the five...
Four different letters are distributed at random into 5 mailboxes by choosing one of the five mailboxes at random for each of the letters. a. Give a possible sample space for this problem. How many points are in the sample space? b. Find the probability that no mailbox contains more than one letter. c. Find the probability that the first two mailboxes are not both empty. Please include explanations throughout your solution. d. Let a random variable X be defined...
Background: In this assignment, you will be implementing Word Guess, a variant of the game Hangman....
Background: In this assignment, you will be implementing Word Guess, a variant of the game Hangman. In this game, a word is first randomly chosen. Initially, the letters in the word are displayed represented by "_”.   For example, if the random word is "yellow”, the game initially displays "_ _ _ _ _ _”. Then, each turn, the player guesses a single letter that has yet to be guessed. If the letter is in the secret word, then the corresponding...
Chapter 19: Stair Framing and Finish Matching Match terms to their definitions. Write the corresponding letters...
Chapter 19: Stair Framing and Finish Matching Match terms to their definitions. Write the corresponding letters on the blanks. Not all terms will be used. 1. the entire stair rail assembly a. baluster 2. railing on a stairway intended to be grasped by the hand to serve as a support and guard b. balustrade c. handrail 3. upright post supporting the handrail in a flight of stairs d. newel post 4. vertical member of a stair rail, usually decorative and...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect on what happens to the families when there is a suicide in the family, based on the Seasons of Love chapter...how should people be told? What details are best left unshared? below is the story These theories may have a certain face-validity, but they often neglect environmental or contextual factors that are innate to answering the question of “why” a person might engage in...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
      MK Restaurant: Branding of Thai-Style Hotpot The restaurant industry is one of the most...
      MK Restaurant: Branding of Thai-Style Hotpot The restaurant industry is one of the most competitive in Thailand. With a large number of players ranging from restaurants in five-star hotels, global fast-food chains to small stalls along the streets and everything in between, the Thais are spoiled for choice. In addition, as the world becomes globalized, consumers are familiar with international dishes and would not hesitate to try new offerings from the other side of the globe. As a...