Clean Palindrome
A text is a clean palindrome if it reads the same forwards and backwards when you look only at letters and digits and ignore upper and lower case. Spaces and punctuation are ignored.
Write is_clean_palindrome(text) that returns True or False. A text with no letters or digits at all counts as a palindrome.
Understand the problem
Turn the rule into exact steps before you code.
Which characters of "Ab, bA!" are kept? Type them in order, in lower case, with no spaces.
Keep letters and digits, ignore case: a, b, b, a. The comma, the space and the exclamation mark are dropped. The kept characters read the same backwards, so the answer is True.
What should is_clean_palindrome("") return?
TrueFalse- It is an error
The task says a text with nothing to compare counts as a palindrome. Empty things are a classic edge case: decide what they mean before you code.
Try examples by hand
Run the steps on two tricky inputs.
Keep only letters and digits of "R2-D2" and lower-case them. What is left?
Dropping the hyphen leaves R2D2, and in lower case r2d2. Digits are kept.
Is "R2-D2" a clean palindrome?
- Yes
- No
The cleaned text r2d2 backwards is 2d2r. They differ, so the answer is No. Note that the original contains 2 and D and 2, which tempts you to say yes if you do not reverse the whole thing.
A friend drops digits and keeps only letters. What does that give for "0P"?
"0p", not a palindrome"p", a palindrome
With digits dropped, 0P becomes p, which reads the same backwards, so the friend would answer True. But digits are part of the text under the rules, so the right answer is False. The right tool is isalnum() (letters and digits), not isalpha().
Write the solution
Clean the text first, then compare it with its reverse.
def is_clean_palindrome(text):
# Write your code here.
pass
Hint 1: a question to ask yourself
Can you write the cleaned version of the text as a separate string first, and only then think about palindromes?
Hint 2: the shape of the answer
Build a list of the lower-cased characters for which isalnum() is true. Then compare it with itself reversed (s[::-1]).
Reflect
Solution
def is_clean_palindrome(text):
cleaned = [ch.lower() for ch in text if ch.isalnum()]
return cleaned == cleaned[::-1]
Another way: two pointers, no extra string
def is_clean_palindrome(text):
left, right = 0, len(text) - 1
while left < right:
if not text[left].isalnum():
left += 1
elif not text[right].isalnum():
right -= 1
elif text[left].lower() != text[right].lower():
return False
else:
left += 1
right -= 1
return True
Pattern: split a vague rule into two exact steps. "Ignore case and punctuation" is not code. "Keep the alphanumeric characters, lower-case them, then compare with the reverse" is. When a rule feels fuzzy, write the transformation down as steps and test each step on its own.
Cost: the first version is O(n) time and O(n) extra space. The two-pointer version is O(n) time and O(1) extra space. You will meet two pointers again in a later chapter.
What an interviewer might ask next: Can you do it without creating a new string? (The second solution does.) Would your answer change for accented letters such as "é"?
VISIBLE = 3
CASES = [
(('A man, a plan, a canal: Panama',), True, 'a famous sentence'),
(('race a car',), False, 'not a palindrome'),
(('Ab, bA!',), True, 'punctuation in the middle'),
(('',), True, 'empty text'),
((' .,! ',), True, 'no letters or digits at all'),
(('0P',), False, 'digits count'),
(('R2-D2',), False, 'digits and a hyphen'),
(('a',), True, 'one letter'),
(('No lemon, no melon',), True, 'upper and lower case mixed'),
(('12321',), True, 'only digits'),
]