Codility - FrogJmp
https://codility.com/programmers/task/frog_jmp
My solution
def solution(x, y, d)
((y - x).to_f / d).ceil
endLearning points
- Paper and pencil (and algebra) helps. The equation is:
x + dn >= yAfter that it was just a matter of findingn. - Integer division can be tricky. Convert to
floatto retain precision, then convert back tointegerafterwards (if the situation requires it). #to_iwill chop off the decimal portion of the float; this may or may not be what you want.O(1)will usually mean there should be no loop used – at all. It is a straightforward computation.