Files
pplus-core/mojo/local/translate-codons.mojo
T
2026-10-05 03:03:38 +00:00

27 lines
839 B
Mojo

# origin: Programming+ exercise translate-codons
# status: unverified; `corpus.py verify` checks it on the pinned compiler
# probe: translate a reading frame into amino acids, stopping at the first stop codon
# expect-stdout: MAK
def translate(t: String) raises -> String:
var stops = ["TAA", "TAG", "TGA"]
var table = {"ATG": "M", "GCC": "A", "AAA": "K", "TTT": "F", "GGC": "G"}
var protein = String("")
var n = t.count_codepoints()
var i = 0
while i + 3 <= n:
var codon = String("")
for j in range(3):
codon += String(t[codepoint=i + j])
if codon in stops:
break
elif codon in table:
protein += table[codon]
else:
protein += "X"
i += 3
return protein^
def main() raises:
print(translate("ATGGCCAAATAGTTT"))