py_echo_validator — Alphabetic Palindrome Check
Difficulty: easy
Write a function that checks if a string is a palindrome, ignoring spaces and case. Only alphabetic characters are considered for the comparison. An empty input (after filtering) is not a palindrome.
def echo_validator(text: str) -> bool:
Examples
>>> echo_validator("racecar")
True
>>> echo_validator("A man a plan a canal Panama")
True
>>> echo_validator("race a car")
False
>>> echo_validator("Was it a car or a cat I saw")
True
>>> echo_validator("hello")
False
>>> echo_validator("Madam Im Adam")
True
>>> echo_validator("")
False
Solution
Download py_echo_validator.pydef echo_validator(text: str) -> bool:
cleaned = [c.lower() for c in text if c.isalpha()]
if not cleaned:
return False
return cleaned == cleaned[::-1]
How It Works
Goal: Determine if the letters-only, case-folded version of the input reads the same forwards and backwards.
Approach: Filter to alphabetic characters, lowercase them, then compare the list to its reverse.
Step by step:
- Build a list of just the alphabetic characters, each lowercased.
- If the filtered list is empty, return
Falseper the spec. - Compare it to its reverse (
[::-1]); equality means palindrome.
Key concept: Normalizing input before comparison — when "equal" means equal under some rule (case-insensitive, ignore punctuation), strip that variance away first so the core check stays simple.