DEV Community

Victor Joseph
Victor Joseph

Posted on

Palindrome Number

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x_copy = x
        array_x = []
        inversed_x = 0

        while x > 0:
            array_x.append(x % 10)
            x = x // 10

        for num in array_x:
            inversed_x *= 10
            inversed_x += num

        if inversed_x == x_copy:
            return True
        return False
Enter fullscreen mode Exit fullscreen mode

Day 3 of 10. Leet code problem can be found here

To solve this problem, i had to reverse the given number by applying the divide (/) and mod (%) operations on it which is then stored in a list (array_x). Now because it is in a list, it cannot be compared with the initial array (x). So i had to convert it to an integer back and then compare the reversed number with original number to see if it is a palindrome. If both original and reversed numbers are equal, then the given number is a palindrome, otherwise it is not a palindrome.

thanks for reading :)

Top comments (0)