Bouncing Ball
Source: May 2025, Task 6 (DTU 02002/02003). Study solution — not an official answer key.
Problem
A ball is dropped from height h0 cm. After each bounce the height is (2/3) of the previous height.
Write bouncing_ball(h0) returning how many bounces occur before the height is below 1 cm.
Example
>>> bouncing_ball(2.5)
3
Approach
Start from h0. Loop: multiply by 2/3, count each bounce, stop when height < 1.
Solution
Show solution Hide solution
Solution
def bouncing_ball(h0):
height = h0
bounces = 0
while True:
height = (2 / 3) * height
bounces += 1
if height < 1:
return bounces