Using Python, reverse an integer, and tell if palindrome

94,434

Solution 1

def palindrome(num):
    return str(num) == str(num)[::-1]

Solution 2

Integer numbers don't have len().

Testing if a number is a palindrome is as simple as testing if the number is equal to its reverse (though if you want maximum efficiency you can just compare characters from both ends of the string until you reach the middle).

To find the reverse of an integer you can either do it the hard way (using mod % and integer division // to find each digit and construct the reverse number):

def reverse(num):
  rev = 0
  while num > 0:
    rev = (10*rev) + num%10
    num //= 10
  return rev

Or the easy way (turning the number into a string, using slice notation to reverse the string and turning it back to an integer):

def reverse(num):
  return int(str(num)[::-1])

Solution 3

This is an unreadable one-line recursive implementation based in part on the answer by pedrosorio.

def reverse(i):
    return int(i!=0) and ((i%10)*(10**int(math.log(i,10))) + reverse(i//10))

def is_palindrome(i):
    return i == reverse(i)

It works for integer i ≥ 0.

Note that reverse(123) == reverse(1230) == 321. This is not a problem, considering any nonzero integer that ends with 0 cannot be a palindrome anyway.

Note also that complete reversal of the integer may of course not be necessary to determine if it's a palindrome. The reversal may be implemented so as to be aborted early if the number is determined to not be a palindrome.

Solution 4

Long but readable:

def palindrome(x):
    a=""
    x=str(x)
    for i in range(len(x),0,-1):
        a+=x[i-1]
        print a
    if a==x:
        return True
    else:
        return False

Solution 5

def revers(num):
    rev = 0
    while num > 0:
        rem = num % 10
        rev = (rev * 10) + rem
        num = num // 10
    return rev  
Share:
94,434
YXH
Author by

YXH

Updated on July 05, 2022

Comments

  • YXH
    YXH almost 2 years

    Using Python, reverse an integer and determine if it is a palindrome. Here is my definition of reverse and palindrome. Do I have a correct logic?

    def reverse(num):
        s=len(num)
        newnum=[None]*length
        for i in num:
            s=s-1
            newnum[s]=i
            return newnum
    
    def palindrome(num):
        a=str(num)
        l=len(z)/2
        if a[:1]==a[-1:][::-1]:
            b=True
        else:
            b=False
    

    I am having some trouble to write def main.

  • Akavall
    Akavall over 11 years
    Your first method is worthy of +1, but it might be useful to point out that we can't reverse a number if it ends with 0. Eg, 1230 would be 321. However, for things like Project Euler where you can be asked to look for palindromes of prime numbers, this methods works fine.
  • pedrosorio
    pedrosorio over 11 years
    @Akavall Thank you for the comment, but I'm not sure if you are implying that the concept of reversing a number ending in 0 is undefined (I think 321 == 0321 is a good definition for the reverse of the number 1230), or if my method can't handle such numbers. If it is the latter, I believe it can. If you are referring to using this to test for palindromicity, numbers ending in 0 can't be palindromes (with the notable exception of 0 itself) and the reverse of such a number according to my definition is always different from the original so we're cool there too, right?
  • Akavall
    Akavall over 11 years
    @pedrosorio, I guess the issue that I was aiming at is that reverse(1230) == reverse(123), since both return 321. But that's an issue that comes up with every method. So I the drawback I was pointing out is not very relevant.
  • Keon Kim
    Keon Kim about 8 years
    what if it is a negative number?
  • Amit Gold
    Amit Gold about 8 years
    @KeonKim I don't think negative numbers can be considered palindromes
  • Asclepius
    Asclepius almost 8 years
    I have written a recursive implementation derived in part from this answer.
  • Toby Speight
    Toby Speight almost 8 years
    Although this code may help to solve the problem, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
  • SamAndrew81
    SamAndrew81 almost 5 years
    I like this one because you can use reverse(num) == num to get a T/F bool for "is palindrome?" The exception being if the integer ends (or begins?) with 0, as aforementioned in the comments here.