# Arup Guha
# 12/8/2020
# Solution to COP 2930 Final Exam Part C Questions 1-3

# Pre-condition: rna is a string with a length that is a multiple
#                of 3 only consisting of the characters A,C,G,U.
# Post-condition: A list of length len(rna)//3 is returned storing
#                 the breakdown of rna into segments of strings of
#                 length 3.
def splitIntoParts(rna):

    # Make an empty list.
    res = []

    # Index into the list skipping by 3's
    for i in range(0, len(rna), 3):

        # Here is how we get a slice that is three characters long.
        res.append(rna[i:i+3])

    # Return
    return res

# Pre-condition: rnafile is a pointer to a file that has 64 lines with each
#                line storing an RNA triple followed by a space followed by
#                the amino acid for that triple.
# Post-condition: a dictionary is returned mapping each triple to the
#                 corresponding amino acid.
def getRNADictionary(rnafile):

    # Make the dictionary.
    rnaDictionary = {}

    # File has exactly 64 lines.
    for i in range(64):

        # Get both tokens.
        toks = rnafile.readline().split()

        # First token is the key, the second the value.
        rnaDictionary[toks[0]] = toks[1]

    # Ta da!
    return rnaDictionary

# Pre-condition: rnalist is a list of 
def getAminoAcidList(rnalist, rnaDictionary):
    
    aaList = []
    for i in range(len(rnalist)):
        aaList.append(rnaDictionary[rnalist[i]])
    return aaList

def main():

    # Here is a string - split into parts.   
    myrnastring = "UCGCAACGGAGAAUUGUUGUGUAG"
    stringParts = splitIntoParts(myrnastring)

    # Set up the rna/amino acid dictionary
    file = open("rna.txt", "r")
    rnaDictionary = getRNADictionary(file)
    file.close()
    
    # Convert our list, and print to see if this worked!
    aaList = getAminoAcidList(stringParts, rnaDictionary)
    print(aaList)

# Run it.
main()


