Sunday, March 18, 2012

exercise 6.6 - is_palindrome


"""==========================
exercise 6.6
=========================="""

def first(word):
    return word[0]

def last(word):
    return word[-1]
    
def middle(word):
    return word[1:-1]

def is_palindrome (word):
    if not isinstance(word, str):
        print("strings only please")
        return False    

    if len(word) >= 3:
        if first(word) == last(word):
            return is_palindrome(middle(word))
        else:
            return False

    if len(word) < 3: # word must have at least 3 letters for middle () to work
        if first(word) == last(word):
            return True
        else:
            return False
    
print "juicy is a palindrome: ", str(is_palindrome("juicy"))
print "noon is a palindrome: ", str(is_palindrome("noon"))

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.