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 “/”.
def solution(X, Y, D):
distance = Y - X
if distance % D == 0:
return distance//D + 1
return (Y – X)/D + ((Y – X) % D == 0 ? 0:1)
The same idea. And infeasible in Python. Anyway, thanks for your sharing.
‘my solution in vb.net….
Thanks for sharing!
But it seems, your solution cannot pass the test.
Solution in C#
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.
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!
Thank you all for sharing!
Quick solution for Pascal (100%)
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.
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.
Because it needs more computational resource. And it is not noticeably easier to understand.
Your code is missing a line for no jump.
If distance is 0, distance % D is 0, and the function will return 0 / D = 0.
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# 🙂
No problem at all. Any solution or discussion about the algorithm is welcome!
Even simpler: (Y – X + D – 1)/D
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…
Similar but probably more random tests resistant solution:
return – (- (Y – X) // D )
I was inspired by https://stackoverflow.com/a/35125872
return (Y – X + D – 1)/D;
My Java solution (100%)
I put the python solution in and got int expected class float found as output
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.
I came up with the same solution but unfortunately it doesn’t pass the 6s time constraint
Java %100 🙂
hi. I know it’s silly question. I don’t understand the program, because I am a beginner. for a first jump, The value of distance is 75, , 75%30 !=0, So 75//31 returns 2 right(remainder is 2). how it returns 3? Actually, I am confusing myself. please answer this.
I do not know your input…. But my guess is that, since 75%30 !=0, we are returning “return distance//D + 1”, that is 3.
For some reason, the dist mod D method and adding one didn’t work for me, broke at higher numbers and failed, so I used VB round up function aka ceiling to get 100.
The solution obtained perfect score.
Detected time complexity:
What about this