Skip to content

Commit a74bd1a

Browse files
committed
feat: Add palindrome check to basic logic problems (Closes #13501)
1 parent e2a78d4 commit a74bd1a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

other/palindrome_check.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Text # Imports the 'Text' type for clarity (optional, but good practice)
2+
3+
def is_palindrome(text: Text) -> bool:
4+
"""
5+
Checks if a string is a palindrome.
6+
7+
A palindrome is a word, phrase, number, or other sequence of characters
8+
which reads the same backward as forward. This implementation is case-sensitive
9+
and includes spaces and punctuation in the check.
10+
11+
Args:
12+
text: The input string to check.
13+
14+
Returns:
15+
True if the string is a palindrome, False otherwise.
16+
17+
Examples (Doctests):
18+
>>> is_palindrome("madam")
19+
True
20+
>>> is_palindrome("racecar")
21+
True
22+
>>> is_palindrome("A man, a plan, a canal: Panama")
23+
False
24+
>>> is_palindrome("level")
25+
True
26+
>>> is_palindrome("hello")
27+
False
28+
"""
29+
# Core logic: The simplest and most Pythonic way to reverse a string
30+
# is using slicing: text[::-1].
31+
# We compare the original string with its reversed version.
32+
return text == text[::-1]
33+
34+
35+
if __name__ == "__main__":
36+
# Standard boilerplate for running documentation examples as tests.
37+
# When this file is run directly (python file.py), it executes the
38+
# doctest examples inside the docstring to verify the function works.
39+
import doctest
40+
doctest.testmod()

0 commit comments

Comments
 (0)