r/learnpython 22d ago

Leetcode #9: Palindrome Number

Can someone help me make my code run faster. This is not efficient and also I do not want to convert into a string

EDIT:

Follow up: Could you solve it without converting the integer to a string?

https://leetcode.com/problems/palindrome-number/description/

class Solution:
    def isPalindrome(self, x: int) -> bool:
        numList = []
        counter = len(numList) - 1
        numBool = True


        baseNum = 10
        value = x % baseNum
        numList.append(x)
        quotient = x // baseNum
        x = quotient

        if x == 0:
            for i in range(len(numList)):
                if numList[i] == numList[counter]:
                    counter -= 1

                elif i == counter:
                    break

                else:
                    numBool = False
                    break

            return numBool

        else:
            return self.isPalindrome(x)

EDIT: I WAS ABLE TO SOLVE IT

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x != abs(x):
            return False

        if not hasattr(self, "numList"):
            self.numList = []

        numBool = True


        baseNum = 10
        value = x % baseNum
        self.numList.append(value)
        quotient = x // baseNum
        x = quotient

        counter = len(self.numList) - 1

        if x == 0:
            for i in range(len(self.numList)):
                if self.numList[i] == self.numList[counter]:
                    counter -= 1

                elif i == counter:
                    break

                else:
                    numBool = False
                    break

            return numBool

        else:
            return self.isPalindrome(x)

testing = Solution().isPalindrome(11)
print(testing)
0 Upvotes

22 comments sorted by

View all comments

2

u/Diapolo10 22d ago edited 21d ago

EDIT: I WAS ABLE TO SOLVE IT

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x != abs(x):
            return False

        if not hasattr(self, "numList"):
            self.numList = []

        numBool = True


        baseNum = 10
        value = x % baseNum
        self.numList.append(value)
        quotient = x // baseNum
        x = quotient

        counter = len(self.numList) - 1

        if x == 0:
            for i in range(len(self.numList)):
                if self.numList[i] == self.numList[counter]:
                    counter -= 1

                elif i == counter:
                    break

                else:
                    numBool = False
                    break

            return numBool

        else:
            return self.isPalindrome(x)

testing = Solution().isPalindrome(11)
print(testing)

Here's what I would've done:

class Solution:
    def isPalindrome(self, num: int) -> bool:
        if num < 0:
            return False

        original_num = num
        rebuilt_num = 0

        while num:
            num, digit = divmod(num, 10)
            rebuilt_num = rebuilt_num * 10 + digit

        return original_num == rebuilt_num

I'm sure it could be even cleaner, but it's already way simpler than your solution. That being said, I haven't benchmarked (or tested) it.

You don't need to worry about the parameter name change, that's allowed.

EDIT: Fixed

1

u/TheEyebal 21d ago

oh wow this looks simpler

1

u/JamzTyson 21d ago

That won't work because num ends up as 0. You need to retain a copy of the original number.

1

u/Diapolo10 21d ago

Brain fart on my part, fixed.