Real palindromes
Write is_palindrome(s) that ignores case, spaces and punctuation, and returns True if the letters and digits read the same forwards and backwards.
is_palindrome("A man, a plan, a canal: Panama")
# True
is_palindrome("race a car")
# False
Show hint
Clean the string first with str.isalnum() and str.lower(), then compare it with its reverse.
Show solution
def is_palindrome(s):
clean = [c.lower() for c in s if c.isalnum()]
return clean == clean[::-1]
Filtering down to lowercase letters and digits turns the messy input into a plain sequence, then a reversed comparison does the rest. O(n) time. A two-pointer version can do it in O(1) extra space if memory matters.
Want a problem like this every day? See today's problem and keep a streak going.