# Solution to Frog-Jmp by codility

15 Jan

Question Name: FrogJmp

This is a very easy question. If with C, one statement is enought.

UPDATE on 2017/11/29: Codility changed Python from 2.X to 3.X. So the integer division needs to be updated to “//” from “/”.

### 33 Replies to “Solution to Frog-Jmp by codility”

1. kosterix says:

return (Y – X)/D + ((Y – X) % D == 0 ? 0:1)

• Sheng says:

The same idea. And infeasible in Python. Anyway, thanks for your sharing.

2. geronimo says:

‘my solution in vb.net….

• Sheng says:

Thanks for sharing!

But it seems, your solution cannot pass the test.

3. Wickedmog says:

Solution in C#

• Dave says:

Please don’t take it personally, I just try to think loud…

This is a perfect example where codility gives 100/100 points for a code, that runs perfectly, but has readability issues. Inappropriate usage of var is pretty subjective, I know. It’s really useful in many cases, but this is a typical situation where it has a negative effect on readability.

Although I am not sure, how could such readability aspects be automatically checked.

• Sheng says:

I am not a C# expert. And I do not know the negative effect of var.

However, I hope this community be open-minded. Any technique discussion without personal attacks is welcome!

4. Annthony says:

Solution in JavaScript

5. Alex Hoang says:

in PHP:

6. Sheng says:

Thank you all for sharing!

7. calinutz says:

Quick solution for Pascal (100%)

8. Dustin says:

These solutions are nice, but modulo is expensive. pseudo: diff := x-y, steps := diff/d, if steps*d < diff: steps++. The multiplication is cheaper than a second division.

• Sheng says:

Sure, there is some space in the original solution to improve. And your suggestion is very awesome!

PS: as long as the solution passed the test, the readability comes first, for the blog post.

9. Sebastian Cheung says:

Why not:

• Sheng says:

Because it needs more computational resource. And it is not noticeably easier to understand.

10. Your code is missing a line for no jump.

• Sheng says:

If distance is 0, distance % D is 0, and the function will return 0 / D = 0.

11. jack says:

PHP

• Sheng says:

Cool! Thanks!

12. Kljuco says:

return (int)Math.Ceiling((Y – X) / (decimal)D);

P.S. I just saw someone posted this solution in PHP. But still… here it is in C# ðŸ™‚

• Sheng says:

No problem at all. Any solution or discussion about the algorithm is welcome!

13. Sandy says:

Even simpler: (Y – X + D – 1)/D

• Son Thai says:

Very good. It’s a well-known trick to integer-divide positive num by positive divisor and round up: (num+(divisor-1))/divisor. This trick was around the time when processors were slow and compilers were dumb…

• Sheng says:

Elegant!

14. Bishal Paudel says:

• Steven says:

15. GiAnG says:

return (Y – X + D – 1)/D;

16. Davide says:

My Java solution (100%)

17. Glenn says:

I put the python solution in and got int expected class float found as output

• Sheng says:

Thanks for your feedback! The solution is updated, since Codility changed from Python 2.X to Python 3.X. The concept is still working well.

18. Daniel says:

Java solution

19. Vinicius Navarro says:

Python Solution:

20. Mustafa Ates says:

Java %100 ðŸ™‚