Exercism: Protein Translation (Python)

感觉是个挺有意思的题。把RNA序列转化成蛋白质名称。不过第一次做的时候,看错题了,以为maping是顺序的,比如(UCU, UCC, UCA, UCG    | Serine), 是必须依次找到4个配对才可以确定是Serine。后来发现没有那么麻烦,mapping实际上是一个set, 任意一个配对都可以map到蛋白质的名字。这样就简单多了。

from enum import Enum

STOP = {"UAA", "UAG", "UGA"}


class Protein(Enum):
    METHIONINE = "Methionine"
    PHENYLALANINE = "Phenylalanine"
    LEUCINE = "Leucine"
    SERINE = "Serine"
    TYROSINE = "Tyrosine"
    CYSTEINE = "Cysteine"
    TRYPTOPHAN = "Tryptophan"


Codens_Protein = {
    "AUG": Protein.METHIONINE,
    "UUU": Protein.PHENYLALANINE,
    "UUC": Protein.PHENYLALANINE,
    "UUA": Protein.LEUCINE,
    "UUG": Protein.LEUCINE,
    "UCU": Protein.SERINE,
    "UCC": Protein.SERINE,
    "UCA": Protein.SERINE,
    "UCG": Protein.SERINE,
    "UAU": Protein.TYROSINE,
    "UAC": Protein.TYROSINE,
    "UGU": Protein.CYSTEINE,
    "UGC": Protein.CYSTEINE,
    "UGG": Protein.TRYPTOPHAN,
}


def proteins(strand):
    sequences = []
    for i in range(int(len(strand)/3)):
        sequences.append(strand[i*3:i*3+3])

    proteins = []
    for seq in sequences:
        if seq in STOP:
            break
        proteins.append(Codens_Protein[seq].value)
    return proteins

看了看其他的解法,我在切配对时做的有些傻了。可以直接用range生成, 不用除index,
    for i in range(0, len(strand), 3)):
        sequences.append(strand[i:i+3])


评论

此博客中的热门博文

拜厄练习曲44, 45

拜厄练习曲38, 39

Exercism: Anagram (Python)