Skip to content

Commit 065af19

Browse files
Refactor and expand is_palindrome test cases
Added clarification comments on `isalnum()` and its implementation. Extended test cases to cover additional edge cases, improving robustness.
1 parent e026fa0 commit 065af19

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Two Pointers/valid_palindrome.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# https://neetcode.io/problems/is-palindrome
22

3+
# `isalnum()` method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
4+
# `isalpha()` method returns True if all the characters are alphabet letters (a-z).
5+
# `isdigit()` method returns True if all the characters are digits (0-9).
6+
# `islower()` method returns True if all the characters are in lower case.
7+
8+
# Therefore, for this problem, we can use `isalnum()` to check if the character is an alphabet letter or a number.
9+
# However, if we are asked to implement `isalnum()` method, we can use the following code:
10+
11+
# def is_alnum(character: str) -> bool:
12+
# return (
13+
# ord('A') <= ord(character) <= ord('Z') or
14+
# ord('a') <= ord(character) <= ord('z') or
15+
# ord('0') <= ord(character) <= ord('9')
16+
# )
17+
318
import unittest
419

520

@@ -34,3 +49,4 @@ class Test(unittest.TestCase):
3449
def test_is_palindrome(self):
3550
self.assertTrue(is_palindrome("Was it a car or a cat I saw?"))
3651
self.assertFalse(is_palindrome("tab a cat"))
52+
self.assertFalse(is_palindrome("0P"))

0 commit comments

Comments
 (0)