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....
-
What type of collaborative venture did Ford enter with Volkswagen? What were the advantages and disadvantages of this venture from Ford's perspective?
-
The asset allocation approach most appropriate for client Kealoha is best described as: A. a surplus optimization approach. B. an integrated assetliability approach. C. a hedging/return-seeking...
-
You have recently won the super jackpot in the Illinois state lottery. On reading the fine print, you discover that you have the following two options: a. You receive \($160,000\) at the beginning of...
-
Weimar Companys income statement information follows. The average number of shares outstanding was 7,800 for 2011 and 6,900 for 2010. Required Compute the following ratios for Weimar for 2011 and...
-
Sunland Company sponsors a defined benefit pension plan. Thecorporation??s actuary provides the following information about theplan.January 1,2025December 31,2025Vested benefit ob 2 answers
-
Johannes Inc. acquired 80 percent of Corner Brook Ltd. common shares on January 1, Year 4, for $720,000. At that date, the fair value of the noncontrolling interest was $180,000. Corner Brooks...
-
In January 2011, The ABC Company received a contract form the Army for 175 electronic devices. The contract was a firm fixed price contract at a unit price of $115,000, and a total price of...
-
Discuss the following questions. What challenges has the amusement industry in the U.S. been facing? How have changing customer demographics affected the theme park industry as a whole? What are the...
-
Garson, Incorporated produces three products. Data concerning the selling prices and unit costs of the three products appear below: F Product G H Selling price $ 70 $ 50 $ 80 Variable costs Fixed...
-
Create a Working with Hive Database -- list available databases show databases; -- create a database for the twitter related analysis create database twitter; -- change to the twitter database use...
-
A person coughs when a foreign object is in the windpipe. The velocity of the cough depends on the size of the object. Suppose a person has a windpipe with a 22-mm radius. If a foreign object has a...
-
Describe a non-profit program. Analyze and present the key challenges facing the program. These challenges might be quite diverse in nature: fiscal, management, inter-organizational, legislative,...
-
Following is a simplified financial position statement for a company. Calculate the liquid ratio. AssetsLiabilities and equity Cash250000Accounts payable880000Trading securities350000Accrued expenses...
-
H.J. Heinzs annual dividends were as follows: 1990 ..............$0.540 1991.............. 0.620 1992 .............. 0.700 1993.............. 0.780 1994 .............. 0.860 1995 .............. 0.940...
-
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...
-
Explain the circumstances under which a person should choose a regular checking account, activity checking account, or interest-bearing checking account.
-
Explain at least three ways to reduce ATM fees charged to your account. Also describe the trade-offs involved in each action. Write About It Create a list with your explanations.
-
Bank X offers a checking account with a minimum balance of \($300\), but does not charge fees nor give interest. Bank Ys minimum balance is \($50\), but it charges a fee of \($25\) if you have more...
Making Money With Chatgpt 10 Ways To Make Money Today 1st Edition - ISBN: 979-8379019761 - Free Book
Study smarter with the SolutionInn App