Skip to main content
2 of 2
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/

Comments from an answer to another question apply here to the commented function reverse_num:

divmod

In Python, when you are performing arithmetic and are interested in both the quotient and the remainder of a division, you can use divmod.

Magic numbers

You have 10 hardcoded in multiple places. My preference is to use a default argument giving the base to use.

def reverse(integer, base=10):
    result = 0
    while integer:
        integer, r = divmod(integer, base)
        result = base * result + r
    return result

More specific comments

  • The documentation for palindrome is somewhat imprecise. It implies that it returns a number but it actually returns a tuple.

  • num = num + rnum can be re-written num += rnum

  • it may be interesting to memoize any result you compute so that you do not it to spend time to compute it later on. This needs some benchmarking to be sure this is relevant.

SylvainD
  • 29.3k
  • 1
  • 47
  • 93