Last active
October 16, 2015 19:39
-
-
Save svalleru/8d33ae5a6cecfd2c584c to your computer and use it in GitHub Desktop.
PalindromeCheck
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
| # 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