I hope this question is okay to ask on Code Review; this is a little function I wrote in Python 2.7.12 that checks if a number is a palindrome. It works fine but I'd like to know if there is a purely mathematical way to do it without using string conversion.

Here is the function:

def is_palindrome(n):
    forward = str(n)
    backward = []

    i = len(forward) - 1
    while i >= 0:
        i -= 1

    backward = "".join(backward)

    if backward == forward:
        return True
        return False

How would one go about solving such a problem without using string conversion?

  • 1
    \$\begingroup\$ divmod(12345, 10)1234, 5 \$\endgroup\$ – Gareth Rees Jan 26 '18 at 14:44
  • \$\begingroup\$ @GarethRees Care to elaborate? \$\endgroup\$ – MadHatter May 24 '18 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.