Hailstones are balls or irregular lumps of ice. They start in a cloud as raindro
ID: 3592049 • Letter: H
Question
Hailstones are balls or irregular lumps of ice. They start in a cloud as raindrops and they are pushed higher and lower in the atmosphere until they freeze many times over and eventually fall on Earth. This problems simulates the sequence of altitude values of a hailstone as it goes through its formation process. 1. Pick a positive integer value for the initial_altitude as the start. 2. If the value of initial_altitude is even, divide it by 2 (or halve it) 3. If the value of initial_altitude is odd, multiply it by 3 and add 1. 4. Continue this process until the value of initial altitude is 1 (we will consider the altitude of the hailstone right before it drops on Earth to be 1) The variable initial_altitude will go through a sequence of values. It will increase and decrease, it will go higher and lower in the atmosphere, but will eventually end at 1 Write a function called noSteps that takes a single argument initial_altitude and returns the number of steps as initial_altitude goes through the hailstone sequence above. Here are some examples of calling the function: >>> print( nosteps(5)) # the sequence of values for initial altitude-5 is: 5, 16, 8, 4, 2, 1, so there will be 6 elements and 5 steps in the sequence For example Test Result print nosteps (5))5 print (noSteps(6)) 8 print (noSteps(2)) 1 print(noSteps(1))Explanation / Answer
def noSteps(n):
count = 0
while n!=1:
if n % 2 == 0:
n = n/2
else:
n = 3*n+1
count = count + 1
return count
print(noSteps(5))
print(noSteps(6))
print(noSteps(2))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.