' For the sake of an exercise, we will implement a function that does a kind...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
' For the sake of an exercise, we will implement a function that does a kind of slice. You must use recursion for this one. Your code is allowed to refer to list index L[0] and also use slice notation L[1:] but no other slices. def take(n, L): 'Returns the list L[0:n].' return [L[0]] + take(n, L[1:]) print(take(4, [1,8,2,3,4,5])) Just knowing the minimum number of coins is not as useful as getting the actual list of coins. Next, write another version of the change function called giveChange that takes the same kind of input as change but returns a list whose first item is the minimum number of coins and whose second item is a list of the coins in that optimal solution. Here's an example: >>> givechange (48, [1, 5, 10, 25, 501) [6, [25, 10, 10, 1, 1, 11] >>> giveChange (48, [1, 7, 24, 42)) 12, [24, 2411 >> givechange (35, [1, 3, 16, 30, 50]) 13, (16, 16, 311 The order in which the coins are presented in the input list doesn't really matter and, similarly, the order in which your solution reports the coins to use is also unimportant: In other words the solution [3, [16, 16, 31] is the same to us as [3, [3, 16, 16] ] or [3, [16, 3, 16] 1. All of these solutions use the same 3 coins after all! This problem may seem challenging at first, but keep in mind that once you establish that giveChange will always return a list of the form [numberofCoins, listofCoins), you can modify your change function relatively modestly to get the giveChange function. First, your base cases must observe the convention and return such a list of the form [numberofCoins, listofCoins]. Then, when you call giveChange recursively, remember that it is returning a list of this fom. Your function will need to pick apart that list to get at the number of coins and the list of coins in that solution. Finally, after deciding whether the use-it or lose-it solution is better, you can prepare your list of the form (numberofCoins, listofcoins] and retum that list. ' For the sake of an exercise, we will implement a function that does a kind of slice. You must use recursion for this one. Your code is allowed to refer to list index L[0] and also use slice notation L[1:] but no other slices. def take(n, L): 'Returns the list L[0:n].' return [L[0]] + take(n, L[1:]) print(take(4, [1,8,2,3,4,5])) Just knowing the minimum number of coins is not as useful as getting the actual list of coins. Next, write another version of the change function called giveChange that takes the same kind of input as change but returns a list whose first item is the minimum number of coins and whose second item is a list of the coins in that optimal solution. Here's an example: >>> givechange (48, [1, 5, 10, 25, 501) [6, [25, 10, 10, 1, 1, 11] >>> giveChange (48, [1, 7, 24, 42)) 12, [24, 2411 >> givechange (35, [1, 3, 16, 30, 50]) 13, (16, 16, 311 The order in which the coins are presented in the input list doesn't really matter and, similarly, the order in which your solution reports the coins to use is also unimportant: In other words the solution [3, [16, 16, 31] is the same to us as [3, [3, 16, 16] ] or [3, [16, 3, 16] 1. All of these solutions use the same 3 coins after all! This problem may seem challenging at first, but keep in mind that once you establish that giveChange will always return a list of the form [numberofCoins, listofCoins), you can modify your change function relatively modestly to get the giveChange function. First, your base cases must observe the convention and return such a list of the form [numberofCoins, listofCoins]. Then, when you call giveChange recursively, remember that it is returning a list of this fom. Your function will need to pick apart that list to get at the number of coins and the list of coins in that solution. Finally, after deciding whether the use-it or lose-it solution is better, you can prepare your list of the form (numberofCoins, listofcoins] and retum that list.
Expert Answer:
Answer rating: 100% (QA)
Consider an Example In 1 giveChange48 15102550 Out1 6 251010111 In 2 giveChange48 172442 Out 2 2 242... View the full answer
Related Book For
Posted Date:
Students also viewed these computer network questions
-
Devise a greedy algorithm that uses the minimum number of towers possible to provide cell service to d buildings located at positions x1, x2, . . . , xd from the start of the road.
-
a. Give a precise expression for the minimum number of nodes in an AVL tree of height h. b. What is the minimum number of nodes in an AVL tree of height 15?
-
a. Give an algorithm to find the minimum number of edges that need to be removed from an undirected graph so that the resulting graph is acyclic. b. Show that this problem is NP-complete for directed...
-
Based on your reading of the rules of Professional Code of Conduct as given by AICPA, which part of the code did the accountants of Enron, Waste Management or Wells Fargo have violated and how about...
-
A nationwide poll claims that the margin of error is no more than 3 percentage points in either direction (i.e., plus or minus) at the 95% confidence level. a. Verify this claim in a particular case...
-
A 5.00-kg block is set into motion up an inclined plane with an initial speed of 8.00 m/s (Fig. P8.33). The block comes to rest after traveling 3.00m along the plane, which is inclined at an angle of...
-
Fill in the Blank. The distribution of several random variables is called ____________ distribution.
-
1. Why does Baxter believe that we should have clear goals in mind in order to answer moral questions about the environment and about pollution in particular? 2. What are the four criteria or goals...
-
www 6. Linus Ullmark of the Boston Bruins had the best save percentage of a goalie in the 2022-2023 season. We take this to mean, loosely, that Ullmark has a 93.8% chance of making a save when a puck...
-
3. Alpha Investors On July 1, 1985, Jim Roberts felt certain the stock market was in for a correction. The S&P 500 Index had risen 14% since the first of the year, and most analysts were predicting...
-
Write a report about different types of heaps, implement at least three of them on a same data of different sizes ; for example: 10000, 100000, and 1000000 numbers, draw graphs and put tables to...
-
Referring to information provided in problem 8, Cardiexs sales in the European Union are projected to remain stable at 100 million for the next 5 years. Show pro-form income and cash-flow statements...
-
The standard deviation of Infosys in Indian rupees (INR) is I = 8. 5 percent, and the standard deviation of the US$/INR exchange rate is s = 5. 5 percent. a. If the correlation between Infosyss asset...
-
Which cost of capital? Guillaume Tel of Ciments Lafarge was unsure about the legitimacy of applying a discount rate of 10. 5 percent to value the acquisition of Hoang Thach Cement Company. After all,...
-
Which of the following would be IDC if incurred in relation to a newly drilled well? a. Labor costs to build a road to the drillsite b. Labor costs to build a road to a producing well c. Cost of a...
-
Fluor Inc. is a U.S.-based global engineering and construction company. Its Brazilian subsidiary earned $112 million in 2012, which is taxed at the Brazilian corporate income tax of 30 percent....
-
Prepare a simple investment plan for a friend who is 35 years old and is currently saving $9,500 per year. This person has just inherited $250,000 in cash. You can make reasonable assumptions about...
-
Review Exhibit 11.4. Analyze each product on the graph according to the characteristics that influence the rate of adoption. For example, what can you conclude from the data about the relative...
-
Assume we have created a packet-switched internet. Using the TCP/IP protocol suite, we need to transfer a huge file. What are the advantage and disadvantage of sending large packets?
-
In a TCP connection, the window size fluctuates between 60,000 bytes and 30,000 bytes. If the average RTT is 30 ms, what is the throughput of the connection?
-
Assume you want to write a program to simulate the permutation boxes in Figure 31.8 in the text. Figure 31.8 a. Show how you represent each box as a table. b. Show the inversion of each box as a...
-
The central difference approximation of \(d^{4} W / d x^{4}-\beta^{4} W=0\) at grid point \(i\) with step size \(h\) is a. \(W_{i+2}-4 W_{i+1}+\left(6-h^{4} \beta^{4} ight) W_{i}-4...
-
What is the difference between explicit and implicit integration methods?
-
Find the solution of a spring-mass-damper system governed by the equation \(m \ddot{x}+c \dot{x}+k x=F(t)=\delta F . t\) with \(m=c=k=1\) and \(\delta F=1\). Assume the initial values of \(x\) and...
Study smarter with the SolutionInn App