[Python] Palindrome number in Python

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    class Solution:
    def isPalindrome(self, x: int) -> bool:
    # If x is a negative number it is not a palindrome
    # If x % 10 = 0, in order for it to be a palindrome the first digit should also be 0
    if x < 0 and x%10 == 0):
    return False

    reversedNum = 0
    while x > reversedNum:
    reversedNum = reversedNum * 10 + x % 10
    x = x // 10

    # If x is equal to reversed number then it is a palindrome
    # If x has odd number of digits, dicard the middle digit before comparing with x
    # Example, if x = 23132, at the end of for loop x = 23 and reversedNum = 231
    # So, reversedNum/10 = 23, which is equal to x
    return True if (x == reversedNum or x == reversedNum // 10) else False


    This is my code which is giving wrong output for 660,
    Expected output : False
    My output : True

    Can someone tell me how to correct this.

    Continue reading...

Compartilhe esta Página