# palindrome.py # Walker M. White (wmw2) # October 8, 2015 """Palindrome functions from second recursion lecture""" import string def ispalindrome(s): """Returns: true if s is a palindrome There are two ways to define a palindrome: 1. s is a palindrome if it reads the same backward and forward. 2. s is a palindrome if either (1) its length is <= 1 OR (2) its first and last chars are the same and the string between them is a palindrome. Letters that differ only in case are considered to match. Precondition s is a string""" assert type(s) == str, str(s) + ' is not a string' # get in the habit # Base palindrome if len(s) < 2: return True # s has at least 2 characters # return (first and last chars match) && (inner substring is pal.) # return equals_ignore_case(s[0], s[-1]) and ispalindrome(s[1:-1]) return s[0] == s[-1] and ispalindrome(s[1:-1]) def ispalindrome2(s): """Returns: true if s is a palindrome There are two ways to define a palindrome: 1. s is a palindrome if it reads the same backward and forward. 2. s is a palindrome if either (1) its length is <= 1 OR (2) its first and last chars are the same and the string between them is a palindrome. Letters that differ only in case are considered to match. Parameter s: the candidate palindrome Precondition s is a string""" assert type(s) == str, str(s) + ' is not a string' # get in the habit return equals_ignore_case(s,reverse(s)) def ispalindrome_loosely(s): """Returns: true if s is a palindrome paying attention only to the letters Case and any non-letter characters are ignored. Parameter s: the candidate palindrome Precondition s is a string""" assert type(s) == str, str(s) + ' is not a string' # get in the habit return ispalindrome2(depunct(s)) def equals_ignore_case(c, d): """Returns: true if strings c and d differ only in case, if at all Parameter c: first string to compare Preconditon: c is a string Parameter c: second string to compare Preconditon: c is a string""" assert type(c) == str, str(c) + ' is not a string' # get in the habit assert type(d) == str, str(d) + ' is not a string' # get in the habit return c.upper() == d.upper() # HELPERS FROM FIRST LECTURE def depunct(s): """Returns: s but with everything that is not a letter removed Parameter: s the string to edit Precondition s is a string""" assert type(s) == str, str(s) + ' is not a string' # get in the habit # s is empty if s == '': return s if not s[0] in string.letters: return depunct(s[1:]) return s[0] + depunct(s[1:]); def reverse(s): """Returns: s with its characters in reverse order Parameter: s the string to reverse Precondition s is a string""" assert type(s) == str, str(s) + ' is not a string' # get in the habit if s == '': return s # s has at least 2 chars return reverse(s[1:]) + s[0]