# My code works, But how do I make this code run in a deterministic time?

The Problem: Given 3 inputs Bounce, Ball drop height, and ball view height.

How do I calculate the number of times the observer can see the ball pass. So my code gives correct output, but it takes a longer time as bounce approaches 1, how do I make it deterministic in nature.

def bouncingBall(h, bounce, window):
BounceFactor = bounce
BallDropHeight = h
ViewerHeight = window
BounceLeft = h
BallSeenTimes = 1
if bounce > 1 or bounce < 0 or window >= h or h ==0:

return -1
else:
while (BounceLeft > ViewerHeight):
BounceLeft = BounceLeft * BounceFactor
if (BounceLeft > ViewerHeight):
BallSeenTimes = BallSeenTimes + 2
else:
break
return BallSeenTimes


I am not looking for code answers, just the direction in which I need to think. Currently, I think If I could somehow establish a linear relationship and create a function to "guess" the passes it would be faster.

• Posted at Stackoverflow at same time. Please do not post the same question on multiple sites. Each community should have an honest shot at answering without anybody's time being wasted. If you don't get a satisfying answer after a week or so, you may flag to request migration. I am voting for closure since there have been several comments to the other posts. – John L. Nov 14 '18 at 16:50
• Duh, I should have searched for existing answers. I just retracted my vote and recommended the other post be deleted. – John L. Nov 14 '18 at 17:27

Your code is determinstic: it uses no randomness. But I assume you want the running time to be essentially independent of the input.

Assuming I've understood the problem correctly, you're dropping a ball from height h and, when dropped from height x, it bounces to height x*Bouncefactor. You want to know how many times it passes through the height you're calling window. Your approach is, essentially, to simulate the bouncing ball and count.

Instead of doing that, you should just calculate how many times the ball bounces before it's at height less than window. This is the least k such that h * Bouncefactor^k < window, so you just need to solve this equation and then convert the number of bounces into the number of times that the ball is seen.

• you have understood the problem correctly, but I do not understand your approach. – Akash Nov 14 '18 at 16:20
• "you should just calculate how many times the ball bounces before it's at height less than window" - This is what I am doing, as bounces below window's height can be ignored. – Akash Nov 14 '18 at 16:21
• Your approach is to simulate the ball bouncing and bouncing and count how many times it's seen. Instead of doing that, you can write an equation that tells you how many times it bounces, and then solve that equation, rather than calculating the height of each bounce. – David Richerby Nov 14 '18 at 16:24
• The number of passes - Directly depends on Initial height and bounce factor. difference in window and Initial height also matter, but I cant understand how I can write it mathematically, any hints? – Akash Nov 14 '18 at 16:27
• The equation is written in my answer! – David Richerby Nov 14 '18 at 16:29