def is_pal(s):
if len(s) < 2:
return True
return s[0] == s[-1] and is_pal(s[1:-1])
def check(text):
clean = "".join(ch.lower() for ch in text if ch.isalnum())
return is_pal(clean)
for t in ["Racecar", "Never odd or even", "Python", "Was it a car or a cat I saw?"]:
print(f"{t!r}: {check(t)}")
Output Verified by running it on Python 3
'Racecar': True
'Never odd or even': True
'Python': False
'Was it a car or a cat I saw?': True
Compare the ends, then recurse on the middle. and short-circuits, so the recursion stops at the first mismatch.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!