Advertisement
DanielKoehler

Untitled

Nov 12th, 2013
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. def isPalindrome(s, ignorecase=False):
  2.    
  3.     """
  4.    >>> type(isPalindrome("bob"))
  5.    <type 'bool'>
  6.    >>> isPalindrome("abc")
  7.    False
  8.    >>> isPalindrome("bob")
  9.    True
  10.    >>> isPalindrome("a man a plan a canal, panama")
  11.    True
  12.    >>> isPalindrome("A man a plan a canal, Panama")
  13.    False
  14.    >>> isPalindrome("A man a plan a canal, Panama", ignorecase=True)
  15.    True
  16.    """
  17.  
  18.     # Create an empty string "onlyLetters"
  19.     # Loop over all characters in the string argument, and add each
  20.     #   character which is a letter to "onlyletters"
  21.  
  22.     # Reverse "onlyletters" and test if this is equal to "onlyletters"
  23.     onlyLetters = ""
  24.     for char in s:
  25.         if char.isalpha():
  26.             onlyLetters += char
  27.  
  28.     if onlyLetters[::-1] == onlyLetters and not ignorecase:
  29.         return True
  30.     elif onlyLetters[::-1].upper() == onlyLetters.upper() and ignorecase:
  31.         return True
  32.     else:
  33.         return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement