Skip to content

Instantly share code, notes, and snippets.

@svalleru
Last active October 16, 2015 19:39
Show Gist options
  • Save svalleru/8d33ae5a6cecfd2c584c to your computer and use it in GitHub Desktop.
Save svalleru/8d33ae5a6cecfd2c584c to your computer and use it in GitHub Desktop.
PalindromeCheck
# Check if a string is palindrome or not
# Ignore all non-letter characters in the string and check should be case-insensitive
#"a"
#"ABba"
#"..a..a"
#"A man, a plan, a canal, Panama!"
#"amanaplanacanalpanama"
#"amanap"..
import re
regex = re.compile('[^a-zA-Z]')
inp_strs = ["a", "ABba", "..a..a", "A man, a plan, a canal, Panama!", "amanaplanacanalpanama", "amanap"]
for i in inp_strs:
#remove non-letter chars & convert to lower case
stripped_str = regex.sub('', i).lower()
if stripped_str == stripped_str[::-1]:
print i, "--is palindrome"
else:
print i, "--is not palindrome"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment