From 01db2631dcf46251bb4fa108078b43a00ae9b152 Mon Sep 17 00:00:00 2001 From: kaviya-ns Date: Tue, 7 Jul 2026 21:50:00 +0530 Subject: [PATCH 1/2] Add is_palindrome_ignore_case_and_spaces to strings/palindrome.py --- strings/palindrome.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/strings/palindrome.py b/strings/palindrome.py index 4df5639b0c49..8e96d1ef63e8 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -81,6 +81,26 @@ 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 and spaces. + 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?") + False + >>> 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 = s.lower().replace(" ", "") + return s == s[::-1] + + 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}" @@ -93,6 +113,7 @@ def benchmark_function(name: str) -> None: for key, value in test_data.items(): assert is_palindrome(key) == is_palindrome_recursive(key) assert is_palindrome(key) == is_palindrome_slice(key) + assert is_palindrome(key) == is_palindrome_ignore_case_and_spaces(key) print(f"{key:21} {value}") print("a man a plan a canal panama") @@ -104,3 +125,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 0.99769 seconds + benchmark_function("is_palindrome_ignore_case_and_spaces") From 2aa4f263f7d16d477084f07f15bc159c3f5a2efd Mon Sep 17 00:00:00 2001 From: kaviya-ns Date: Thu, 9 Jul 2026 12:02:58 +0530 Subject: [PATCH 2/2] Address review: handle punctuation, add dedicated test data for new function --- strings/palindrome.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/strings/palindrome.py b/strings/palindrome.py index 8e96d1ef63e8..f2f49d004e75 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -83,13 +83,13 @@ def is_palindrome_slice(s: str) -> bool: def is_palindrome_ignore_case_and_spaces(s: str) -> bool: """ - Return True if s is a palindrome, ignoring case and spaces. + 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?") - False + True >>> is_palindrome_ignore_case_and_spaces("Hello World") False >>> is_palindrome_ignore_case_and_spaces("Never Odd or Even") @@ -97,10 +97,19 @@ def is_palindrome_ignore_case_and_spaces(s: str) -> bool: >>> is_palindrome_ignore_case_and_spaces("") True """ - s = s.lower().replace(" ", "") + 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}" @@ -113,17 +122,18 @@ def benchmark_function(name: str) -> None: for key, value in test_data.items(): assert is_palindrome(key) == is_palindrome_recursive(key) assert is_palindrome(key) == is_palindrome_slice(key) - assert is_palindrome(key) == is_palindrome_ignore_case_and_spaces(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 + # finished 500,000 runs in 1.90863 seconds benchmark_function("is_palindrome_slice") - # finished 500,000 runs in 0.85234 seconds + # finished 500,000 runs in 2.80057 seconds benchmark_function("is_palindrome") - # finished 500,000 runs in 1.32028 seconds + # finished 500,000 runs in 4.50983 seconds benchmark_function("is_palindrome_recursive") - # finished 500,000 runs in 2.08679 seconds + # finished 500,000 runs in 6.81874 seconds benchmark_function("is_palindrome_traversal") - # finished 500,000 runs in 0.99769 seconds + # finished 500,000 runs in 10.64074 seconds benchmark_function("is_palindrome_ignore_case_and_spaces")