Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3.0 help!! Purpose: To practice recursion on a problem that is ill-suited

ID: 3848932 • Letter: P

Question

Python 3.0 help!!

Purpose: To practice recursion on a problem that is ill-suited to using loops. Degree of Difficulty: Moderate Aspiring pokemon trainer Ash Ketchum has caught quite a tewpokemon. In order to keep them healthy, he regularly feeds vitamins to his pokemon. However, the vitamins are usually bigger than what a pokemon can swallow in one bite, and so when Ash's pokemon team is given a vitamin, they will break it apart into pieces that are small enough for them to eat. Sometimes, though, the vitamin is SO big, that the pokemon have to break it apart more than once! Here is how the process works: If the vitamin'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 for 1 pokemon. Otherwise, the pokemon will team up to smash the vitamin into pieces of equal weight. The number of new pieces seems to be random (the pokemon can be a bit excitable), 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 pokemon will then break each of these pieces again until they are small enough to swallow. For example, suppose the initial vitamin weighed 0.6 grams. The pokemon break it once, and we randomly determine that the vitamin breaks into 2 parts (each part weighs 0.3 grams). The pokemon 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 pokemon 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 pokemon 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. So in this case, 10 different pokemon are able to get their vitamin dosage from a single, original 0.6 gram vitamin. 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. For this question, your task is to write a recursive program that will calculate how many edible vitamin pieces are made whenever a pokemon team is given a vitamini that weighs W grams. (a) Write a recursive function that simulates the breaking of a single vitamin. The weight of the vitamin (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 vitamin. To write this function, you will need to use random numbers for when the vitamin is broken into parts. If you first import random as rand, then the expression rand.randint(a, b) will give you a random number in the range from a to h (including a and b). (b) Write a few lines of code that asks the user for the size of a vitamin, 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 vitamin. An example of your program running might look like this: How big is the vitamin, in grams? 1.0 On average, a pokemon team can get 18.774 bite-sized pieces from a 1 gram vitamin 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 vitamins of weight 5, 10 and 100 grams. Copy/paste your output for all of the examples above into a document called a4q4_output.txt for submission.

Explanation / Answer

VITAMINS.py

import random

def vitamins(weight,quantity=1, pieces=1): # positional arguments to pass values among recursive calls
   if weight <= 0.1:           # Base condition when weight is less than 0.1 then the quantity is calculated
       quantity *= pieces       # shorthand for quantity = quantity * pieces
       return quantity
   else:  
       breakdown = random.randint(2,4)
       piece = pieces*breakdown   # piece to hold the total pieces of each new piece
       weight = weight/breakdown     # shorthand for weight = weight/breakdown
       return vitamins(weight, pieces=piece)   # recursive call to the function
       # The pieces variable contains total pieces

size = float(input('How big is the vitamin in grams ? : ')) # Take input from the user

# Simulation for 1000 times
edible_pieces = list()
i = 0
while(i < 1000):               # To call the function 1000 times and saving the result
   edible_pieces.append(vitamins(size))   # A list to hold the values return by function
   i += 1                   # To limit the condition of loop
average = sum(edible_pieces) / len(edible_pieces)   # logic to find average sum() function calculates sum of all the elements in the list

print('On average , a pokemon team can get {0} bite-sized pieces from a {1} gm vitamin !'.format(average, size))

PLEASE RATE!! Thank You!