1

Not sure what to do here, anyone know what's up? I'm trying to find if a number is a palindrome, and I'm getting hung up with the input -121. The output should be 'false', as in it is not a palindrome, whereas 121 is.

LeetCode code:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x = str(x)
        xrev = x[::-1]
        if x == xrev:
            return 'true'
        else:
            return 'false'

This returns 'true', 'false' is expected. Whereas my code ran on Atom returns 'false' as expected:

def isPalindrome(x):
    x = str(x)
    xrev = x[::-1]
    if x == xrev:
        return 'true'
    else:
        return 'false'

print(isPalindrome(-121))
19
  • 1
    Welcome to SO! Why are you returning strings that happen to have the words "true" and "false" in them instead of boolean values True and False? LC is probably treating "false" as truthy. Try running this code in your repl: bool("false"). It's best to avoid bare booleans anyway, you can just return str(x) == str(x)[::-1]. Commented Aug 26, 2021 at 22:39
  • 4
    Since your method has type hint -> bool it definitely should not be returning strings. Commented Aug 26, 2021 at 22:43
  • There is no case where a negative number is a palindrome, so you should really start with if x < 0 check, or read the conditions of your LeetCode site that probably say the input is always x > 0 Commented Aug 26, 2021 at 22:45
  • 2
    @OneCricketeer Why make it more complicated? Commented Aug 26, 2021 at 22:50
  • @don'ttalkjustcode The "correct" (i.e. optimal) way to do this is to not use a string and instead modulo and divide by 10's to get the individual integers to compare Commented Aug 27, 2021 at 0:01

2 Answers 2

2

As @khelwood in the comments pointed out, since the LeetCode code is hinted to return a boolean, the string value is automatically converted to a boolean value, which only if the string is empty, will return False.

Corrected code:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x = str(x)
        xrev = x[::-1]
        if x == xrev:
            return True
        else:
            return False
Sign up to request clarification or add additional context in comments.

2 Comments

This worked, thanks! I thought I should have used 'true' and 'false' because of the requested outputs by LC.
@Sam this can be simplified even further. When you see if condition: return True else: return False you can always replace it with return condition. Or worst case, return bool(condition).
0

try this, instead of true and false return 1 and 0:

class Solution:
    def isPalindrome(self, x) -> bool:
        x = str(x)
        xrev = x[::-1]
        if x == xrev:
            return 1
        else:
            return 0

1 Comment

Did you know that True==1 and False==0 by definition? While your code might work, it's better to be explicit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.