Check whether a phrase reads the same backwards
Your task
A word puzzle site accepts a phrase as a palindrome when its letters and digits read the same in both directions. Spaces, punctuation and case do not count.
Write is_clean_palindrome(s) that returns whether s is a palindrome by that rule.
Input and output:
s: a string of 0 to 200,000 printable ASCII characters, codes 32 to 126.- Returns true or false.
Rules:
- Keep only the letters
atozandAtoZand the digits0to9. Ignore every other character, including spaces, punctuation and_. - Compare letters without regard to case, so
Amatchesa. A digit matches only the same digit. - Return true when the kept characters read the same forwards and backwards.
- A string with no letters or digits, including the empty string, returns true.
Examples
is_clean_palindrome(s: str) -> boolExample 1
Input
is_clean_palindrome("A man, a plan, a canal: Panama")Output
TrueExample 2
Input
is_clean_palindrome("No 'x' in Nixon")Output
True