Skip to content

Instantly share code, notes, and snippets.

@babjo
Created July 23, 2016 06:54
Show Gist options
  • Save babjo/021f8359611bd581d119be94e1fffcf7 to your computer and use it in GitHub Desktop.
Save babjo/021f8359611bd581d119be94e1fffcf7 to your computer and use it in GitHub Desktop.
# Define a procedure is_palindrome, that takes as input a string, and returns a
# Boolean indicating if the input string is a palindrome.

# Base Case: '' => True
# Recursive Case: if first and last characters don't match => False
# if they do match, is the middle a palindrome?

def is_palindrome(s):
    if s == '':
        return True
    else:
        return s[0] == s[-1] and is_palindrome(s[1:-1])
 
print is_palindrome('')
#>>> True
print is_palindrome('abab')
#>>> False
print is_palindrome('abba')
#>>> True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment