Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def isPalindrome(s, ignorecase=False):
- """
- >>> type(isPalindrome("bob"))
- <type 'bool'>
- >>> isPalindrome("abc")
- False
- >>> isPalindrome("bob")
- True
- >>> isPalindrome("a man a plan a canal, panama")
- True
- >>> isPalindrome("A man a plan a canal, Panama")
- False
- >>> isPalindrome("A man a plan a canal, Panama", ignorecase=True)
- True
- """
- # Create an empty string "onlyLetters"
- # Loop over all characters in the string argument, and add each
- # character which is a letter to "onlyletters"
- # Reverse "onlyletters" and test if this is equal to "onlyletters"
- onlyLetters = ""
- for char in s:
- if char.isalpha():
- onlyLetters += char
- if onlyLetters[::-1] == onlyLetters and not ignorecase:
- return True
- elif onlyLetters[::-1].upper() == onlyLetters.upper() and ignorecase:
- return True
- else:
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement