return this code with only one Value def flight(height): """ recursively calculates the path of a hailstone :param heigh

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

return this code with only one Value def flight(height): """ recursively calculates the path of a hailstone :param heigh

Post by answerhappygod »

return this code with only one Value
def flight(height):
"""
recursively calculates the path of a
hailstone
:param height: the height of the
hailstone
:return: a recursive call, which at the end
returns the number
of "steps"taken for the
hailstone to reach a height of 1
"""
#### BASE CASES:
# if height is zero or lower, print out an
"invalid" message and return 0
if height<=0:
print("Invalid height
of",height)
return 0
# stops when it reachs height of 1 (the
ground)
if height == 1:
print("Height of",height)
return (flight,), 1
#### RECURSIVE CASES:
# if the current height is even, divide it by
2
if height % 2 == 0:
print("Height of",height)
rest =
flight(height//2)
#odd
else:
print("Height of",height)
rest =
flight(height*3+1)
return (height,) + rest[0], rest[1] +
1
if __name__ == "__main__":
print("Welcome to the Hailstone
Simulator!")
msg = "Please enter a height for
the hailstone to start at: "
start_height = int(input(msg))
steps=flight(start_height)
if steps!=0:
steps=steps[-1]-1
# recursive call goes here
print("\nIt took", steps, "steps to hit
the ground.")
print("Thank you for using the Hailstone
Simulator!\n")
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply