Codility 'FrogJmp' Solution

Martin Kysel · July 23, 2014

Short Problem Definition:

Count minimal number of jumps from position X to Y.

FrogJmp

Complexity:

expected worst-case time complexity is O(1)

expected worst-case space complexity is O(1).

Execution:

Do not use float division if possible!

Solution:

def solution(X, Y, D):
    if Y < X or D <= 0:
        raise Exception("Invalid arguments")
        
    if (Y- X) % D == 0:
        return (Y- X) // D
    else:
        return ((Y- X) // D) + 1

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.