Skip to content

Instantly share code, notes, and snippets.

@panki
Created May 11, 2020 22:58
Show Gist options
  • Save panki/de22073a652bc30700f90c58c0a2c996 to your computer and use it in GitHub Desktop.
Save panki/de22073a652bc30700f90c58c0a2c996 to your computer and use it in GitHub Desktop.
Palindrome checker
# * 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
# * 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