Skip to main content
added 158 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Method 1 sometimes gives wrong solution because it has a bug:

def solution(X, Y, D):
    if (X == Y):
        jumps = 0
    elif Y-X % D == 0:
        jumps = (Y-X)/D
    else:
        jumps = ((Y-X)/D) + 1
    return jumps

In the elif there, Y-X should have been within brackets, because % has higher precedence than - operator. In other words, Y - X % D is not the same as (Y - X) % D.

Other than that, they are practically equivalent. I would clean up the formatting and remove some obvious temporary variables, like this:

def solution(X, Y, D):
    jumps = (Y - X) / D
    if (Y - X) % D > 0:
        return jumps + 1
    return jumps

In particular, in the if instead of incrementing with jumps += 1, I return the value immediately, because I prefer to avoid mutating variables in general.

Method 1 sometimes gives wrong solution because it has a bug:

def solution(X, Y, D):
    if (X == Y):
        jumps = 0
    elif Y-X % D == 0:
        jumps = (Y-X)/D
    else:
        jumps = ((Y-X)/D) + 1
    return jumps

In the elif there, Y-X should have been within brackets, because % has higher precedence than - operator. In other words, Y - X % D is not the same as (Y - X) % D.

Other than that, they are practically equivalent. I would clean up the formatting and remove some obvious temporary variables, like this:

def solution(X, Y, D):
    jumps = (Y - X) / D
    if (Y - X) % D > 0:
        return jumps + 1
    return jumps

Method 1 sometimes gives wrong solution because it has a bug:

def solution(X, Y, D):
    if (X == Y):
        jumps = 0
    elif Y-X % D == 0:
        jumps = (Y-X)/D
    else:
        jumps = ((Y-X)/D) + 1
    return jumps

In the elif there, Y-X should have been within brackets, because % has higher precedence than - operator. In other words, Y - X % D is not the same as (Y - X) % D.

Other than that, they are practically equivalent. I would clean up the formatting and remove some obvious temporary variables, like this:

def solution(X, Y, D):
    jumps = (Y - X) / D
    if (Y - X) % D > 0:
        return jumps + 1
    return jumps

In particular, in the if instead of incrementing with jumps += 1, I return the value immediately, because I prefer to avoid mutating variables in general.

Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Method 1 sometimes gives wrong solution because it has a bug:

def solution(X, Y, D):
    if (X == Y):
        jumps = 0
    elif Y-X % D == 0:
        jumps = (Y-X)/D
    else:
        jumps = ((Y-X)/D) + 1
    return jumps

In the elif there, Y-X should have been within brackets, because % has higher precedence than - operator. In other words, Y - X % D is not the same as (Y - X) % D.

Other than that, they are practically equivalent. I would clean up the formatting and remove some obvious temporary variables, like this:

def solution(X, Y, D):
    jumps = (Y - X) / D
    if (Y - X) % D > 0:
        return jumps + 1
    return jumps