DEV Community

Victor Joseph
Victor Joseph

Posted on

1 1

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 :)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay