Skip to main content
added 486 characters in body
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93

janos' answer is good.

Let's me add a few simple independant comments on top of his solution :

  • Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Thus, as D is supposed to be positive, (Y - X) % D >= 0. Therefore, your check could be written if (Y - X) % D != 0 or in a more Pythonic way : if (Y - X) % D.

  • Python has a pretty cool divmod function. It computes quotient and reminder which is exactly what you want here. Your function becomes :

     def solution(X, Y, D):
         q, r = divmod(Y-X, D)
         if r > 0:
             return q + 1
         return q
    
  • You could avoid the repeted return jump by using the ternary operator : return jumps + (1 if (Y - X) % D > 0 else 0). Also, from the Python 3 doc and the Python 2 doc:

The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

thus, you can write this : return jumps + bool((Y - X) % D > 0).

By taking all comments into account, your code becomes :

def solution2(X, Y, D):
    q, r = divmod(Y-X, D)
    return q + bool(r)

It cannot really get any simpler, can it ?


It seems like I dove into the code before turning my brain on.

You can use math.ceil and everything will go fine :

import math
def solution2(X, Y, D):
    return math.ceil((Y-X)/float(D))

Note that I used a bit of a hack to have floating number division. In Python 3, this is the default behavior for division and you can have it easily with from __future__ import division.

janos' answer is good.

Let's me add a few simple independant comments on top of his solution :

  • Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Thus, as D is supposed to be positive, (Y - X) % D >= 0. Therefore, your check could be written if (Y - X) % D != 0 or in a more Pythonic way : if (Y - X) % D.

  • Python has a pretty cool divmod function. It computes quotient and reminder which is exactly what you want here. Your function becomes :

     def solution(X, Y, D):
         q, r = divmod(Y-X, D)
         if r > 0:
             return q + 1
         return q
    
  • You could avoid the repeted return jump by using the ternary operator : return jumps + (1 if (Y - X) % D > 0 else 0). Also, from the Python 3 doc and the Python 2 doc:

The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

thus, you can write this : return jumps + bool((Y - X) % D > 0).

By taking all comments into account, your code becomes :

def solution2(X, Y, D):
    q, r = divmod(Y-X, D)
    return q + bool(r)

It cannot really get any simpler, can it ?

janos' answer is good.

Let's me add a few simple independant comments on top of his solution :

  • Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Thus, as D is supposed to be positive, (Y - X) % D >= 0. Therefore, your check could be written if (Y - X) % D != 0 or in a more Pythonic way : if (Y - X) % D.

  • Python has a pretty cool divmod function. It computes quotient and reminder which is exactly what you want here. Your function becomes :

     def solution(X, Y, D):
         q, r = divmod(Y-X, D)
         if r > 0:
             return q + 1
         return q
    
  • You could avoid the repeted return jump by using the ternary operator : return jumps + (1 if (Y - X) % D > 0 else 0). Also, from the Python 3 doc and the Python 2 doc:

The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

thus, you can write this : return jumps + bool((Y - X) % D > 0).

By taking all comments into account, your code becomes :

def solution2(X, Y, D):
    q, r = divmod(Y-X, D)
    return q + bool(r)

It cannot really get any simpler, can it ?


It seems like I dove into the code before turning my brain on.

You can use math.ceil and everything will go fine :

import math
def solution2(X, Y, D):
    return math.ceil((Y-X)/float(D))

Note that I used a bit of a hack to have floating number division. In Python 3, this is the default behavior for division and you can have it easily with from __future__ import division.

added 636 characters in body
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93

janos' answer is good.

Let's me add a few simple independant comments on top of his solution :

  • Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Thus, as D is supposed to be positive, (Y - X) % D >= 0. Therefore, your check could be written if (Y - X) % D != 0 or in a more Pythonic way : if (Y - X) % D.

  • Python has a pretty cool divmod function. It computes quotient and reminder which is exactly what you want here. Your function becomes :

     def solution(X, Y, D):
         q, r = divmod(Y-X, D)
         if r > 0:
             return q + 1
         return q
    
  • You could avoid the repeted return jump by using the ternary operator : return jumps + (1 if (Y - X) % D > 0 else 0). Also, from the Python 3 doc and the Python 2 doc:

The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

thus, you can write this : return jumps + bool((Y - X) % D > 0).

By taking all comments into account, your code becomes :

def solution2(X, Y, D):
    q, r = divmod(Y-X, D)
    return q + bool(1 if r else 0)

It cannot really get any simpler, can it ?

janos' answer is good.

Let's me add a few simple independant comments on top of his solution :

  • Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Thus, as D is supposed to be positive, (Y - X) % D >= 0. Therefore, your check could be written if (Y - X) % D != 0 or in a more Pythonic way : if (Y - X) % D.

  • Python has a pretty cool divmod function. It computes quotient and reminder which is exactly what you want here. Your function becomes :

     def solution(X, Y, D):
         q, r = divmod(Y-X, D)
         if r > 0:
             return q + 1
         return q
    
  • You could avoid the repeted return jump by using the ternary operator : return jumps + (1 if (Y - X) % D > 0 else 0).

By taking all comments into account, your code becomes :

def solution2(X, Y, D):
    q, r = divmod(Y-X, D)
    return q + (1 if r else 0)

It cannot really get any simpler, can it ?

janos' answer is good.

Let's me add a few simple independant comments on top of his solution :

  • Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Thus, as D is supposed to be positive, (Y - X) % D >= 0. Therefore, your check could be written if (Y - X) % D != 0 or in a more Pythonic way : if (Y - X) % D.

  • Python has a pretty cool divmod function. It computes quotient and reminder which is exactly what you want here. Your function becomes :

     def solution(X, Y, D):
         q, r = divmod(Y-X, D)
         if r > 0:
             return q + 1
         return q
    
  • You could avoid the repeted return jump by using the ternary operator : return jumps + (1 if (Y - X) % D > 0 else 0). Also, from the Python 3 doc and the Python 2 doc:

The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

thus, you can write this : return jumps + bool((Y - X) % D > 0).

By taking all comments into account, your code becomes :

def solution2(X, Y, D):
    q, r = divmod(Y-X, D)
    return q + bool(r)

It cannot really get any simpler, can it ?

Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93

janos' answer is good.

Let's me add a few simple independant comments on top of his solution :

  • Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Thus, as D is supposed to be positive, (Y - X) % D >= 0. Therefore, your check could be written if (Y - X) % D != 0 or in a more Pythonic way : if (Y - X) % D.

  • Python has a pretty cool divmod function. It computes quotient and reminder which is exactly what you want here. Your function becomes :

     def solution(X, Y, D):
         q, r = divmod(Y-X, D)
         if r > 0:
             return q + 1
         return q
    
  • You could avoid the repeted return jump by using the ternary operator : return jumps + (1 if (Y - X) % D > 0 else 0).

By taking all comments into account, your code becomes :

def solution2(X, Y, D):
    q, r = divmod(Y-X, D)
    return q + (1 if r else 0)

It cannot really get any simpler, can it ?