Skip to content

Commit 074a3f3

Browse files
authored
Add palindrome checker logic building problem
This module provides a function to check if a string is a palindrome, including examples and doctest integration.
1 parent 036660a commit 074a3f3

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Palindrome Checker.
2+
3+
This module contains functions to check if a string is a palindrome.
4+
A palindrome is a word, phrase, or sequence that reads the same backward as forward.
5+
"""
6+
7+
8+
def is_palindrome(text: str) -> bool:
9+
"""
10+
Check if a given string is a palindrome.
11+
12+
A palindrome is a string that reads the same forward and backward,
13+
ignoring case, spaces, and punctuation.
14+
15+
Args:
16+
text: The string to check
17+
18+
Returns:
19+
True if the string is a palindrome, False otherwise
20+
21+
Examples:
22+
>>> is_palindrome("racecar")
23+
True
24+
>>> is_palindrome("hello")
25+
False
26+
>>> is_palindrome("A man a plan a canal Panama")
27+
True
28+
>>> is_palindrome("race a car")
29+
False
30+
>>> is_palindrome("")
31+
True
32+
>>> is_palindrome("a")
33+
True
34+
>>> is_palindrome("Madam")
35+
True
36+
"""
37+
# Remove non-alphanumeric characters and convert to lowercase
38+
cleaned = "".join(char.lower() for char in text if char.isalnum())
39+
# Check if the cleaned string equals its reverse
40+
return cleaned == cleaned[::-1]
41+
42+
43+
if __name__ == "__main__":
44+
import doctest
45+
46+
doctest.testmod()

0 commit comments

Comments
 (0)