# palindromes.py
# Lillian Lee (LJL2), Walker White (WMW2)
# Mar 19, 2014

"""Recursion demo with RNA as motivation"""



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.

    Precondition s is a string"""

    # Last character is s[len(s)-1]
    lastindex = len(s)-1
    return len(s) <= 1 or (s[0] == s[lastindex] and ispalindrome(s[1:lastindex]))



# lists of base pairs and their complements
base = ['A', 'C', 'G', 'U']
comp = ['U', 'G', 'C', 'A']

def iscomplement(c1,c2):
    """Returns: True if c1 and c2 are complementary base pairs, False o.w.
    Pre: c1 and c2 are each one of 'A', 'C', 'G', 'U'"""
    pass
    return base.index(c1) == comp.index(c2)

def is5Ahairpin(s):
    """Returns: True if string s represents a hairpin RNA sequence with
    a loop consisting of 5 As, False otherwise.
    Pre: s is a (possibly empty) string of As, Cs, Gs, and Us."""
    # Assume we have access to function iscomplement(x,y)
    pass

    # If A has > 5 characters, it's a recursion candidate.
    # (Technically, we could also be checking for odd length and
    # rejecting such s's immediately, but that's a less
    # generalizable approach.)

    if len(s) <= 5:
        return s == 'A'*5
    else:
        lastindex = len(s)-1
        return iscomplement(s[0], s[lastindex]) and is5Ahairpin(s[1:lastindex])