-
Notifications
You must be signed in to change notification settings - Fork 1
/
bouncing_balls.py
41 lines (27 loc) · 1.07 KB
/
bouncing_balls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def bouncingBall(h, bounce, window):
"""A child plays with a ball on the nth floor of a big building.
The height of this floor is known:
(float parameter "h" in meters, h > 0) .
He lets out the ball. The ball rebounds for example to two-thirds:
(float parameter "bounce", 0 < bounce < 1)
of its height.
His mother looks out of a window that is 1.5 meters from the ground:
(float parameters window < h).
How many times will the mother see the ball either falling or
bouncing in front of the window
(return a positive integer unless conditions are not fulfilled
in which case return -1) ?
Note
You will admit that the ball can only be seen if the height
of the rebouncing ball is stricty greater than the window parameter.
Example:
h = 3, bounce = 0.66, window = 1.5, result is 3
h = 3, bounce = 1, window = 1.5, result is -1
"""
if h <= 0 or bounce <= 0 or bounce >= 1 or window >= h:
return -1
count = 1
while h * bounce > window:
h *= bounce
count += 2
return count