File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Learn Python together
2+ """A child is playing with a ball on the nth floor of a tall building.
3+ The height of this floor above ground level, h, is known.
4+ He drops the ball out of the window. The ball bounces (for example),
5+ to two-thirds of its height (a bounce of 0.66).
6+ His mother looks out of a window 1.5 meters from the ground.
7+ How many times will the mother see the ball pass in front of
8+ her window (including when it's falling and bouncing?
9+ Three conditions must be met for a valid experiment:
10+ Float parameter "h" in meters must be greater than 0
11+ Float parameter "bounce" must be greater than 0 and less than 1
12+ Float parameter "window" must be less than h.
13+ If all three conditions above are fulfilled,
14+ return a positive integer, otherwise return -1."""
15+
16+ def bouncing_ball (h , bounce , window ):
17+ # check if input values are valid
18+ if h <= 0 or bounce <= 0 or bounce >= 1 or window >= h :
19+ return - 1
20+ count = 1 # count the first pass
21+ height = h * bounce # height of the first bounce
22+
23+ # keep bouncing the ball until it stops bouncing higher than the window
24+ while height > window :
25+ count += 2 # count the upward and downward passes
26+ height *= bounce # height of the next bounce
27+
28+ return count
You can’t perform that action at this time.
0 commit comments