@@ -81,6 +81,26 @@ def is_palindrome_slice(s: str) -> bool:
8181 return s == s [::- 1 ]
8282
8383
84+ def is_palindrome_ignore_case_and_spaces (s : str ) -> bool :
85+ """
86+ Return True if s is a palindrome, ignoring case and spaces.
87+ Otherwise return False.
88+
89+ >>> is_palindrome_ignore_case_and_spaces("A man a plan a canal Panama")
90+ True
91+ >>> is_palindrome_ignore_case_and_spaces("Was it a car or a cat I saw?")
92+ False
93+ >>> is_palindrome_ignore_case_and_spaces("Hello World")
94+ False
95+ >>> is_palindrome_ignore_case_and_spaces("Never Odd or Even")
96+ True
97+ >>> is_palindrome_ignore_case_and_spaces("")
98+ True
99+ """
100+ s = s .lower ().replace (" " , "" )
101+ return s == s [::- 1 ]
102+
103+
84104def benchmark_function (name : str ) -> None :
85105 stmt = f"all({ name } (key) == value for key, value in test_data.items())"
86106 setup = f"from __main__ import test_data, { name } "
@@ -93,6 +113,7 @@ def benchmark_function(name: str) -> None:
93113 for key , value in test_data .items ():
94114 assert is_palindrome (key ) == is_palindrome_recursive (key )
95115 assert is_palindrome (key ) == is_palindrome_slice (key )
116+ assert is_palindrome (key ) == is_palindrome_ignore_case_and_spaces (key )
96117 print (f"{ key :21} { value } " )
97118 print ("a man a plan a canal panama" )
98119
@@ -104,3 +125,5 @@ def benchmark_function(name: str) -> None:
104125 benchmark_function ("is_palindrome_recursive" )
105126 # finished 500,000 runs in 2.08679 seconds
106127 benchmark_function ("is_palindrome_traversal" )
128+ # finished 500,000 runs in 0.99769 seconds
129+ benchmark_function ("is_palindrome_ignore_case_and_spaces" )
0 commit comments