1
\$\begingroup\$

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:
        backward.append(forward[i])
        i -= 1

    backward = "".join(backward)

    if backward == forward:
        return True
    else:
        return False

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

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.