Created
May 11, 2020 22:58
-
-
Save panki/de22073a652bc30700f90c58c0a2c996 to your computer and use it in GitHub Desktop.
Palindrome checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# * palindrome checker | |
# "aba" | |
# "racecar" | |
# "a_* b?a" | |
# "Aba" | |
from string import ascii_lowercase | |
def check(s): | |
if len(s) <= 1: | |
return True | |
l = 0 | |
r = len(s) - 1 | |
while l < r: | |
lchr = s[l].lower() | |
while lchr not in ascii_lowercase and l < r: | |
l += 1 | |
lchr = s[l].lower() | |
rchr = s[r].lower() | |
while rchr not in ascii_lowercase and r > l: | |
r -= 1 | |
rchr = s[r].lower() | |
if lchr != rchr: | |
return False | |
l += 1 | |
r -= 1 | |
return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# * palindrome checker | |
# "aba" | |
# "racecar" | |
# "a_* b?a" | |
# "Aba" | |
def check(s): | |
if len(s) <= 1: | |
return True | |
l = 0 | |
r = len(s) - 1 | |
while l < r: | |
lchr = s[l].lower() | |
while lchr not in 'a..z' and l < r: | |
l += 1 | |
rchr = s[r].lower() | |
while rchr not in 'a..z' and r > l: | |
r -= 1 | |
if lchr != rchr: | |
return False | |
l += 1 | |
r -= 1 | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment