Hello readers! It’s really important for me to continue my practice and learning. Lately, I have been completing a coding challenge a day with codewars. This helps me keep my skills sharp and see how other devs all over the planet would solve these problems. Being able to see other solutions expands my mind for solving future problems in a clever and efficient way. I’ve decided that I am going to post my solutions here written in Java, JavaScript and Python for all of my readers to learn! Please let me know if you have any questions.
Bouncing Balls
A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known.
He drops the ball out of the window. The ball bounces (for example), to two-thirds of its height (a bounce of 0.66).
His mother looks out of a window 1.5 meters from the ground.
How many times will the mother see the ball pass in front of her window (including when it’s falling and bouncing?
Three conditions must be met for a valid experiment:
- Float parameter “h” in meters must be greater than 0
- Float parameter “bounce” must be greater than 0 and less than 1
- Float parameter “window” must be less than h.
If all three conditions above are fulfilled, return a positive integer, otherwise return -1.
Note:
The ball can only be seen if the height of the rebounding ball is strictly 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
(Condition 2) not fulfilled).
I was particularly excited about this problem because I was hoping that I would stumble on one that allowed me to use recursion. This one was a lot of fun to solve:
JavaScript:
function bouncingBall(h, bounce, window) {
// Qualify the condition that must be met
const cond = h > 0 && (bounce > 0 && bounce < 1) && window < h;
// Check the condition
if (cond) {
// If condition is met, the ball will be seen once going up...
// And once coming down, as well as the first time its dropped
return h < window ? -1 : 2 + bouncingBall((h * bounce), bounce, window);
} else {
// If condition is not met, per instructions, return -1
return -1;
}
}
Python:
def bouncingBall(h, bounce, window):
if not (h > 0 and (bounce > 0 and bounce < 1) and window < h):
return -1
else:
return 1 if h < window else 2 + bouncingBall((h * bounce), bounce, window)
Java:
public class BouncingBall {
public static int bouncingBall(double h, double bounce, double window) {
Boolean cond = h > 0 && (bounce > 0 && bounce < 1) && window < h;
if (cond) {
return h < window ? -1 : 2 + bouncingBall((h * bounce), bounce, window);
}
return -1;
}
}
Disclaimer: *Now, as much as I am tempted to use ES8 & ES9 functions to reduce the number of lines in JavaScript, I am always a fan of readable code that others can understand. Therefore, with the JavaScript examples, I will try to write it without fancy functions as much as possible so it can actually be read and absorbed by my readers. All of my solutions are commented to explain my thinking. Of course, they could always be better. Please feel free to share how you would solve it!*
All credit for the problem sets go to codewars