Answered step by step
Verified Expert Solution
Question
1 Approved Answer
To practice recursion on a problem that is ill-suited to using loops. Degree of Difficulty: Moderate. In the southern continent of the planet Moosetopia, where
To practice recursion on a problem that is ill-suited to using loops. Degree of Difficulty: Moderate. In the southern continent of the planet Moosetopia, where the climate is wet and chilly, lives a small but formidable animal called a moonxter. The moonxters' main food source is a delicious mushroom-like fun- gus called a kuuki, which can grow to be quite large. In order for a family of moonxters to eat a kuuki, the kuuki has to be broken into pieces that are small enough to eat. The moonxter family group gathers around the kuuki and grips it with their teeth, pulling the kuuki into pieces. Eventually, these pieces will become small enough to eat by moonxters. The details of the way the kuuki breaks into edible pieces are as follows: If the kuuki's weight is less than or equal to 0.1 grams, it is small enough to eat, and doesn't need to be broken. Thus, there is 1 edible piece. Otherwise, the moonxter family will grip the kuuki by the edges, and pull the kuuki apart into pieces of equal weight. The number of new pieces seems to be random, either 2, 3, or 4, but the weight of each piece is the same (one-half, one-third or one-quarter of the previous piece). The family of moonxters will then break each of these pieces until they are small enough to eat. Example For example, suppose the initial kuuki weighed 0.6 grams. The moonxters break it once, and we randomly determine that the kuuki breaks into 2 parts (each part weighs 0.3 grams). The moonxters break the first 0.3 gram piece, which breaks into 3 pieces. Since these pieces will each weigh 0.1 grams, they are now edible, so we have 3 edible pieces so far. The moonxters will then break the second 0.3 gram piece; this time, it breaks into 2 parts, each of which weighs 0.15 grams, which are still too large to eat. The moonxters break each of these parts again; the first breaks into 3 parts, each weighing 0.05 grams, so we have 3 more pieces. The second breaks into 4 parts, each weighing 0.0375 grams, so that's another 4 pieces. There are no longer any pieces larger than 0.1 grams, so the total number of edible pieces is 3 + 3+ 4 = 10 pieces. Tracing through the problem like in the paragraph above is exhausting! But if you bring yourself to trust in the power of recursion, solving this problem is not hard at all. Task Breakdown For this question, your task is to write a recursive program that will calculate how many edible pieces are hade whenever a family of moonxters tries to eat a kuuki that weighs W grams. (a) Write a recursive function that simulates the breaking of a single kuuki. The weight of the kuuki (as a float) should be a parameter to your function. The function should return an integer, indicating the number of edible pieces produced from breaking the kuuki. You are not allowed to use loops for this part of the program. You will need to use random numbers for when the kuuki is broken into parts. The expression rand.randint(a,b) will give you a random number in the range from a to b inclusively on both sides. For example, urn either 2, 3, or 4, with an equal random chance. Recall, to use rand.randint(a,b) you will need to include import random as rand at the top of your program. (b) Write a few lines of code that asks the user for the size of a kuuki, and uses a loop that will call your piece-counting recursive function 1000 times. Use the results of those 1000 simulations to report the average number of edible pieces produced from a kuuki. An example of your program running might look like this: How big is the kuuki, in grams? 1.0 On average, a moonxter family can get 18.774 edible pieces from a 1 gram kuuki! Note that because of the randomness involved you might never get this exact result with an input of 1.0 grams but it should be pretty close. Run your program using kuukis of weight 5, 10 and 100 grams. Copy/paste your output for all of the examples above into a document called a7q2_output.txt for submission. Yes, the plural of kuuki is kuukis.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started