Add is_palindrome_ignore_case_and_spaces to strings/palindrome.py#14907
Add is_palindrome_ignore_case_and_spaces to strings/palindrome.py#14907kaviya-ns wants to merge 3 commits into
Conversation
Abhirai2006
left a comment
There was a problem hiding this comment.
Nice small addition, thanks! Two things I noticed though:
The doctest with "Was it a car or a cat I saw?" returns False, but that's kind of an accident — it's only False because the trailing "?" doesn't get stripped (the function only removes spaces, not punctuation). If you actually remove the "?", that phrase is a real palindrome ignoring case/spaces. So the test passes but not really for the reason you'd want it to. Might be worth swapping in a cleaner example, or handling punctuation too so the function does what its name says.
Also the new assert in main (is_palindrome(key) == is_palindrome_ignore_case_and_spaces(key)) is checked against the existing test_data, but none of those strings have spaces or mixed case — so it's not really testing the new behavior, just repeating the old check. Adding one or two mixed-case/spaced entries would make that assertion actually mean something.
Otherwise looks good, happy to approve once these are sorted.
|
Hi @Abhirai2006 , Thanks for the feedback! I've updated the function to also strip punctuation, so 'Was it a car or a cat I saw?' is now a genuine positive example rather than an accidental one. Also added a separate test_data_ignore_case_and_spaces dict with mixed-case, spaced, and punctuated entries, and an assert loop in main, so the new behavior is actually being tested rather than just repeating the existing checks. |
zain-cs
left a comment
There was a problem hiding this comment.
Logic looks correct and all doctests check out — left two minor notes inline.
| return s == s[::-1] | ||
|
|
||
|
|
||
| def is_palindrome_ignore_case_and_spaces(s: str) -> bool: |
There was a problem hiding this comment.
Nice implementation and the docstring covers it well. Small naming nit: since .isalnum() also strips punctuation, not just case and spaces, might be worth reflecting that in the function name too (or it's fine as-is if the broader scope was intentional).
There was a problem hiding this comment.
The broader scope (also stripping punctuation) was intentional. Since a fully descriptive name would get pretty long, I added a note in the docstring clarifying it also ignores punctuation, so the behavior is explicit there. Let me know if you'd still like me to rename it and I can update it.
| assert is_palindrome_ignore_case_and_spaces(key) == value | ||
| print("a man a plan a canal panama") | ||
|
|
||
| # finished 500,000 runs in 0.46793 seconds |
There was a problem hiding this comment.
These benchmark timing changes for is_palindrome, is_palindrome_slice, and is_palindrome_recursive look unrelated to this PR's actual purpose — was this from a re-run on a different machine? Might be worth reverting these lines to keep the diff focused just on the new function.
There was a problem hiding this comment.
thanks! . those were leftover from a local re-run on my machine. I've reverted them back to the original values.
Describe your change:
Adds a new function
is_palindrome_ignore_case_and_spacestostrings/palindrome.pythat checks whether a string is a palindrome while ignoring letter case and spaces.
Existing functions in the file are unchanged.
Checklist: