Write a program for solving summation puzzles by enumerating and testing all possible configurations. Using your...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a program for solving summation puzzles by enumerating and testing all possible configurations. Using your program, solve the three puzzles given in Section 5.3.3. 5.3.3 Multiple Recursion Generalizing from binary recursion, we define multiple recursion as a process in which a method may make more than two recursive calls. Our recursion for an- alyzing the disk space usage of a file system (see Section 5.1.4) is an example of multiple recursion, because the number of recursive calls made during one invoca- tion was equal to the number of entries within a given directory of the file system. Another common application of multiple recursion is when we want to enumer- ate various configurations in order to solve a combinatorial puzzle. For example, the following are all instances of what are known as summation puzzles: pot + pan dog + cat boy + girl bib pig baby To solve such a puzzle, we need to assign a unique digit (that is, 0, 1.....9) to each letter in the equation, in order to make the equation true. Typically, we solve such a puzzle by using our human observations of the particular puzzle we are trying to solve to eliminate configurations (that is, possible partial assignments of digits to letters) until we can work through the feasible configurations that remain, testing for the correctness of each one. If the number of possible configurations is not too large, however, we can use a computer to simply enumerate all the possibilities and test each one, without em- ploying any human observations. Such an algorithm can use multiple recursion to work through the configurations in a systematic way. To keep the description general enough to be used with other puzzles, we consider an algorithm that enu- merates and tests all k-length sequences, without repetitions, chosen from a given universe U. We show pseudocode for such an algorithm in Code Fragment 5.11. building the sequence of k elements with the following steps: Recursively generating the sequences of k 1 elements 2. Appending to each such sequence an element not already contained in it. Throughout the execution of the algorithm, we use a set U to keep track of the elements not contained in the current sequence, so that an element e has not been used yet if and only if e is in U. Another way to look at the algorithm of Code Fragment 5.11 is that it enumer- ates every possible size-k ordered subset of U, and tests each subset for being a possible solution to our puzzle. For summation puzzles. U (0.1.2.3.4.5.6.7.8.9) and each position in the sequence corresponds to a given letter. For example, the first position could stand for b, the second for o, the third for y, and so on. Algorithm PuzzleSolve(k, S, U): Input: An integer k, sequence S, and set U Output: An enumeration of all k-length extensions to S using elements in U without repetitions for each e in U do Add e to the end of S Remove e from U if k== 1 then Test whether S is a configuration that solves the puzzle if S solves the puzzle then add S to output (a solution) (a recursive call) (e is now considered as unused) Code Fragment 5.11: Solving a combinatorial puzzle by enumerating and testing all possible configurations. else PuzzleSolve (k - I, S, U) Remove e from the end of S Add e back to U In Figure 5.14, we show a recursion trace of a call to PuzzleSolve(3, S, U), where S is empty and U= (a,b,c). During the execution, all the permutations of the three characters are generated and tested. Note that the initial call makes three recursive calls, each of which in turn makes two more. If we had executed PuzzleSolve(3, S, U) on a set U consisting of four elements, the initial call would have made four recursive calls, each of which would have a trace looking like the one in Figure 5.14. PuzzleSolve(2, a. (b.c)) PuzzleSolve(Lab. (c)) initial call PuzzleSolvet), () [ab.c)) PuzzleSolve(2, b. Jac))) PuraleSolvr(1, ba, (c)) (e is now being used) PuraleSolve(, {b]] PuzzleSolve(2. < [a,bl) PuzzleSahver(1, ca. [b]) PuzzleSolve(t bc. 1H PizaleSolve( cb. (a)), chu Figure 5.14: Recursion trace for an execution of PuzzleSolve(3, S. U), where Sis empty and 7-a.b.c). This execution generates and tests all permutations of a, b, and c. We show the permutations generated directly below their respective boxes. Write a program for solving summation puzzles by enumerating and testing all possible configurations. Using your program, solve the three puzzles given in Section 5.3.3. 5.3.3 Multiple Recursion Generalizing from binary recursion, we define multiple recursion as a process in which a method may make more than two recursive calls. Our recursion for an- alyzing the disk space usage of a file system (see Section 5.1.4) is an example of multiple recursion, because the number of recursive calls made during one invoca- tion was equal to the number of entries within a given directory of the file system. Another common application of multiple recursion is when we want to enumer- ate various configurations in order to solve a combinatorial puzzle. For example, the following are all instances of what are known as summation puzzles: pot + pan dog + cat boy + girl bib pig baby To solve such a puzzle, we need to assign a unique digit (that is, 0, 1.....9) to each letter in the equation, in order to make the equation true. Typically, we solve such a puzzle by using our human observations of the particular puzzle we are trying to solve to eliminate configurations (that is, possible partial assignments of digits to letters) until we can work through the feasible configurations that remain, testing for the correctness of each one. If the number of possible configurations is not too large, however, we can use a computer to simply enumerate all the possibilities and test each one, without em- ploying any human observations. Such an algorithm can use multiple recursion to work through the configurations in a systematic way. To keep the description general enough to be used with other puzzles, we consider an algorithm that enu- merates and tests all k-length sequences, without repetitions, chosen from a given universe U. We show pseudocode for such an algorithm in Code Fragment 5.11. building the sequence of k elements with the following steps: Recursively generating the sequences of k 1 elements 2. Appending to each such sequence an element not already contained in it. Throughout the execution of the algorithm, we use a set U to keep track of the elements not contained in the current sequence, so that an element e has not been used yet if and only if e is in U. Another way to look at the algorithm of Code Fragment 5.11 is that it enumer- ates every possible size-k ordered subset of U, and tests each subset for being a possible solution to our puzzle. For summation puzzles. U (0.1.2.3.4.5.6.7.8.9) and each position in the sequence corresponds to a given letter. For example, the first position could stand for b, the second for o, the third for y, and so on. Algorithm PuzzleSolve(k, S, U): Input: An integer k, sequence S, and set U Output: An enumeration of all k-length extensions to S using elements in U without repetitions for each e in U do Add e to the end of S Remove e from U if k== 1 then Test whether S is a configuration that solves the puzzle if S solves the puzzle then add S to output (a solution) (a recursive call) (e is now considered as unused) Code Fragment 5.11: Solving a combinatorial puzzle by enumerating and testing all possible configurations. else PuzzleSolve (k - I, S, U) Remove e from the end of S Add e back to U In Figure 5.14, we show a recursion trace of a call to PuzzleSolve(3, S, U), where S is empty and U= (a,b,c). During the execution, all the permutations of the three characters are generated and tested. Note that the initial call makes three recursive calls, each of which in turn makes two more. If we had executed PuzzleSolve(3, S, U) on a set U consisting of four elements, the initial call would have made four recursive calls, each of which would have a trace looking like the one in Figure 5.14. PuzzleSolve(2, a. (b.c)) PuzzleSolve(Lab. (c)) initial call PuzzleSolvet), () [ab.c)) PuzzleSolve(2, b. Jac))) PuraleSolvr(1, ba, (c)) (e is now being used) PuraleSolve(, {b]] PuzzleSolve(2. < [a,bl) PuzzleSahver(1, ca. [b]) PuzzleSolve(t bc. 1H PizaleSolve( cb. (a)), chu Figure 5.14: Recursion trace for an execution of PuzzleSolve(3, S. U), where Sis empty and 7-a.b.c). This execution generates and tests all permutations of a, b, and c. We show the permutations generated directly below their respective boxes.
Expert Answer:
Related Book For
Business Communication Essentials a skill based approach
ISBN: 978-0132971324
6th edition
Authors: Courtland L. Bovee, John V. Thill
Posted Date:
Students also viewed these programming questions
-
In Problems 5158, use a calculator to evaluate each expression. Round your answer to three decimal places. In 5 3
-
Consider the digital divide and how Smart Phones are increasing Internet access to the negative side of Smart Phones. Is there a balance between the advantages and disadvantages of this technology?...
-
Buffett seeks out companies with conservative financing, such as companies with total liabilities relative to total assets that are below the median for their respective industry, low debt to...
-
For the Somerset furniture Company described in Case Problem 10.1 in Chapter 10, determine the product lead time by developing a time line from the initiation of a purchase order to product delivery....
-
In Fig P6.107 the pipe is galvanized iron. Estimate the percentage increase in the flow rate (a) if the pipe entrance is cut off flush with the wall and (b) if the butterfly valve is opened wide....
-
Consider the refinement to the external sort algorithm that produces runs of length 2B on average, where B is the number of buffer pages. This refinement was described in Section 11.2.1 under the...
-
Why are proof test plans needed?
-
Why is it important for an organization to have an information security policy?
-
Provide a statement of need and "hypothetical" data substantiating the need of remote work during a pandemic.The data should clearly serve as a basis for the selection of the program content.
-
Kleen Corp., a privately owned and operated single-stream recycling facility, has annual contracts with several cities in the Tri-County Metropolitan Area. Kleen Corp. wants to add a new set of...
-
Listed below are year-end account balances ($ in millions) taken from the records of Symphony Stores. Debit Credit Accounts receivable 671 Building and equipment 924 Cash 47 Interest receivable 42...
-
Grasshopper made earnings to shareholders of $8,250,000 in 2019 and $8,889,000 in 2020. The company shares capital was 12 million ordinary shares of $1 each in both years Calculate the EPS for 2019...
-
6. Two aircraft manufacturers are interacting as in Cournot model we discussed in class. (7) For simplicity, assume that two firms produce perfect substitutes. Market demand is p = 21-Q, where Q = Q1...
-
Your company sponsors a 4 0 1 ( k ) plan into which you deposit 1 1 percent of your $ 1 2 1 , 0 0 0 annual income. Your company matches 7 7 percent of the first 1 0 percent of your earnings. You...
-
Question 1 For all questions in this quiz, consider the following Cournot duopoly set-up. Two price-making firms have marginal costs given, respectively, by: MC = 25 +0.20Q1, and MC2 = 25+ 0.60Q2....
-
Share Your Group Findings Discussion Board: Theoretical Position of Developmental Theories Select and respond by answering: Does the theory fall under nature, nurture, or both? Why? What evidence...
-
If y= 2x (Inx), then y' = ? a) 2x (Inx) b) 2x (Inx) c) 2 + Inx e) none of these
-
9.Consider the reaction 3NO2(g)+H2O=2HNO3(aq)+NO(g) where Delta H=-137 kJ.How many kilojoules are released when 92.3g of NO2 reacts?
-
Your company's error cost an important business customer a new client; you know it, and your customer knows it. Do you apologize, or do you refer to the incident in a positive light without admitting...
-
Several members of your sales team are protesting the company's "business casual" dress code, claiming that dressing nicely makes them feel awkward and overly formal in front of customers. You have...
-
Select a piece of business mail that you received at work or at home. Analyze its appearance. What nonverbal messages does this piece send? Are these messages consistent with the content of the...
-
Reflect on the two communication theories presented in this chapter and write down which one best reflect your approach to communication and why.
-
Find two celebrities from sports or entertainment; one whom you admire and one you are critical of. Write down what you like and dislike of them respectively and discuss the extent to which their...
-
Define strategic communication?
Study smarter with the SolutionInn App