Below code works fine with VScode but running the same on Leetcode compiler got this error
int
object is not iterable for the loop for i in s:
def isPalindrome(s):
rev = ""
#print(s)
for i in s:
rev = i rev
#print(rev)
if (rev == s):
return True
else:
return False
print(isPalindrome("-121"))```
TypeError: 'int' object is not iterable
for i in s:
Line 4 in isPalindrome (Solution.py)
ret = Solution().isPalindrome(param_1)
Line 32 in _driver (Solution.py)
_driver()
Line 42 in <module> (Solution.py)
Can anyone point out what am i missing here? I dont thing the code is incorrect here.
CodePudding user response:
Maybe you should try to use a list explicitly instead of the string. Here is an example:
def isPalindrome(s):
return s == s[::-1]
print(isPalindrome(list("-121")))
CodePudding user response:
Apparently, Leetcode gives you real number type. You needs to add s = str(s)
to ensure the input is a string.