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))
"true"and"false"in them instead of boolean valuesTrueandFalse? 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 justreturn str(x) == str(x)[::-1].-> boolit definitely should not be returning strings.if x < 0check, or read the conditions of your LeetCode site that probably say the input is alwaysx > 0