Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions strings/palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"AB": False,
}
# Ensure our test data is valid
assert all((key == key[::-1]) is value for key, value in test_data.items())
assert all((key == key[::-1]) == value for key, value in test_data.items())


def is_palindrome(s: str) -> bool:
"""
Return True if s is a palindrome otherwise return False.

>>> all(is_palindrome(key) is value for key, value in test_data.items())
>>> all(is_palindrome(key) == value for key, value in test_data.items())
True
"""

Expand All @@ -41,7 +41,7 @@ def is_palindrome_traversal(s: str) -> bool:
"""
Return True if s is a palindrome otherwise return False.

>>> all(is_palindrome_traversal(key) is value for key, value in test_data.items())
>>> all(is_palindrome_traversal(key) == value for key, value in test_data.items())
True
"""
end = len(s) // 2
Expand All @@ -60,7 +60,7 @@ def is_palindrome_recursive(s: str) -> bool:
"""
Return True if s is a palindrome otherwise return False.

>>> all(is_palindrome_recursive(key) is value for key, value in test_data.items())
>>> all(is_palindrome_recursive(key) == value for key, value in test_data.items())
True
"""
if len(s) <= 1:
Expand All @@ -75,14 +75,14 @@ def is_palindrome_slice(s: str) -> bool:
"""
Return True if s is a palindrome otherwise return False.

>>> all(is_palindrome_slice(key) is value for key, value in test_data.items())
>>> all(is_palindrome_slice(key) == value for key, value in test_data.items())
True
"""
return s == s[::-1]


def benchmark_function(name: str) -> None:
stmt = f"all({name}(key) is value for key, value in test_data.items())"
stmt = f"all({name}(key) == value for key, value in test_data.items())"
setup = f"from __main__ import test_data, {name}"
number = 500000
result = timeit(stmt=stmt, setup=setup, number=number)
Expand All @@ -91,8 +91,8 @@ def benchmark_function(name: str) -> None:

if __name__ == "__main__":
for key, value in test_data.items():
assert is_palindrome(key) is is_palindrome_recursive(key)
assert is_palindrome(key) is is_palindrome_slice(key)
assert is_palindrome(key) == is_palindrome_recursive(key)
assert is_palindrome(key) == is_palindrome_slice(key)
print(f"{key:21} {value}")
print("a man a plan a canal panama")

Expand Down