Skip to content
Snippets Groups Projects
Select Git revision
  • ffd634ddd7bb6921dc7b8b4a1caed3f5aa4f6b9e
  • main default protected
2 results

palindrome.py

Blame
  • Christopher Bohn's avatar
    Christopher Bohn authored
    ffd634dd
    History
    palindrome.py 712 B
    def is_palindrome(text):
        return is_palindrome_helper(text, 0, len(text)-1)
    
    
    def is_palindrome_helper(text, start, end):
        if start >= end:
            return True
    
        first = text[start].lower()
        last = text[end].lower()
    
        if first.isalpha() and last.isalpha():
            if first == last:
                return is_palindrome_helper(text, start+1, end-1)
            else:
                return False
        elif not last.isalpha():
            return is_palindrome_helper(text, start, end-1)
        else:
            return is_palindrome_helper(text, start+1, end)
    
    
    print(is_palindrome('Madam, I\'m Adam'))
    print(is_palindrome('Go hang a salami, I\'m a lasagna hog'))
    print(is_palindrome('A man, a plan, a canal–Panama!'))