-
-
Notifications
You must be signed in to change notification settings - Fork 50.8k
Add is_palindrome_ignore_case_and_spaces to strings/palindrome.py #14907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,35 @@ def is_palindrome_slice(s: str) -> bool: | |
| return s == s[::-1] | ||
|
|
||
|
|
||
| def is_palindrome_ignore_case_and_spaces(s: str) -> bool: | ||
| """ | ||
| Return True if s is a palindrome, ignoring case, spaces, and punctuation. | ||
| Otherwise return False. | ||
|
|
||
| >>> is_palindrome_ignore_case_and_spaces("A man a plan a canal Panama") | ||
| True | ||
| >>> is_palindrome_ignore_case_and_spaces("Was it a car or a cat I saw?") | ||
| True | ||
| >>> is_palindrome_ignore_case_and_spaces("Hello World") | ||
| False | ||
| >>> is_palindrome_ignore_case_and_spaces("Never Odd or Even") | ||
| True | ||
| >>> is_palindrome_ignore_case_and_spaces("") | ||
| True | ||
| """ | ||
| s = "".join(char.lower() for char in s if char.isalnum()) | ||
| return s == s[::-1] | ||
|
|
||
|
|
||
| test_data_ignore_case_and_spaces = { | ||
| "A man a plan a canal Panama": True, | ||
| "Was it a car or a cat I saw?": True, | ||
| "Hello World": False, | ||
| "Never Odd or Even": True, | ||
| "": True, | ||
| } | ||
|
|
||
|
|
||
| def benchmark_function(name: str) -> None: | ||
| stmt = f"all({name}(key) == value for key, value in test_data.items())" | ||
| setup = f"from __main__ import test_data, {name}" | ||
|
|
@@ -94,6 +123,8 @@ def benchmark_function(name: str) -> None: | |
| assert is_palindrome(key) == is_palindrome_recursive(key) | ||
| assert is_palindrome(key) == is_palindrome_slice(key) | ||
| print(f"{key:21} {value}") | ||
| for key, value in test_data_ignore_case_and_spaces.items(): | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! . those were leftover from a local re-run on my machine. I've reverted them back to the original values. |
||
|
|
@@ -104,3 +135,5 @@ def benchmark_function(name: str) -> None: | |
| benchmark_function("is_palindrome_recursive") | ||
| # finished 500,000 runs in 2.08679 seconds | ||
| benchmark_function("is_palindrome_traversal") | ||
| # finished 500,000 runs in 4.27493 seconds | ||
| benchmark_function("is_palindrome_ignore_case_and_spaces") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.