Middle
Task: Palindrome Write a function is_palindrome(s: str) -> bool that checks whether a string is a palindrome (reads the same from left to right and right to left). Ignore spaces, punctuation, and case. Examples: - is_palindrome("racecar") → True - is_palindrome("A man, a plan, a canal: Panama") → True - is_palindrome("race a car") → False
sobes.tech AI
Answer from AI
def is_palindrome(s: str) -> bool: import re # Remove all non-letter and non-digit characters, and convert to lowercase cleaned = re.sub(r'[^a-zA-Z0-9]', '', s).lower() # Check if the string is equal to its reverse return cleaned == cleaned[::-1]
Examples:
print(is_palindrome("racecar")) # True print(is_palindrome("A man, a plan, a canal: Panama")) # True print(is_palindrome("race a car")) # False