Public EcoRouter Knowledge · AI-generated content ·
Write a Python function that checks whether a string is a palindrome, ignoring spaces, punctuation and case.
The question
Write a Python function that checks whether a string is a palindrome, ignoring spaces, punctuation and case. Include a few test cases.
Balanced
Here is a Python function that checks for a palindrome while ignoring spaces, punctuation, and case:
python
import string
def is_palindrome(text: str) -> bool:
"""
Checks if a string is a palindrome, ignoring case, spaces, and punctuation.
"""
# Filter to alphanumeric characters and convert to lowercase
cleaned = [char.lower() for char in text if char.isalnum()]
# Check if the cleaned list is equal to its reverse
return cleaned == cleaned[::-1]
# --- Test Cases ---
if __name__ == "__main__":
test_cases = [
("A man, a plan, a canal: Panama!", True),
("Race a car", False),
("Was it a car or a cat I saw?", True),
("No 'x' in Nixon", True),
("Hello, World!", False),
("12321", True),
("123421", False),
("", True), # An empty string is trivially a palindrome
]
for phrase, expected in test_cases:
result = is_palindrome(phrase)
status = "✓ PASS" if result == expected else "✗ FAIL"
print(f"{status} | Input: {phrase!r} -> Result: {result}")How it works:
char.isalnum()checks whether each character is a letter or number, stripping out spaces and punctuation symbols.char.lower()ensures comparisons are case-insensitive.cleaned == cleaned[::-1]compares the processed sequence to its reversed version.
Answering this used an estimated 75% less than EcoRouter’s configured frontier baseline
- Routed to
- Balanced
- Tokens
- 241 in / 510 out
- Cost
- $0.0021
- Baseline
- $0.0084
Figures recorded by EcoRouter when this answer was generated, and fixed at that moment. Cost comparisons are against a configured reference model, not a measurement of electricity, carbon or water.
Ask about this
Ask EcoRouter a follow-up using this Knowledge as context. Nothing becomes public unless you choose to publish it.
Ask a follow-up0 views
Comments
No comments yet.