diff --git a/exercises/practice/protein-translation/.meta/tests.toml b/exercises/practice/protein-translation/.meta/tests.toml index 5fb1890..b465aed 100644 --- a/exercises/practice/protein-translation/.meta/tests.toml +++ b/exercises/practice/protein-translation/.meta/tests.toml @@ -87,11 +87,15 @@ description = "Translation stops if STOP codon in middle of three-codon sequence [2c2a2a60-401f-4a80-b977-e0715b23b93d] description = "Translation stops if STOP codon in middle of six-codon sequence" +[f6f92714-769f-4187-9524-e353e8a41a80] +description = "Sequence of two non-STOP codons does not translate to a STOP codon" + [1e75ea2a-f907-4994-ae5c-118632a1cb0f] description = "Non-existing codon can't translate" [9eac93f3-627a-4c90-8653-6d0a0595bc6f] description = "Unknown amino acids, not part of a codon, can't translate" +reimplements = "1e75ea2a-f907-4994-ae5c-118632a1cb0f" [9d73899f-e68e-4291-b1e2-7bf87c00f024] description = "Incomplete RNA sequence can't translate" diff --git a/exercises/practice/protein-translation/protein-translation-test.arr b/exercises/practice/protein-translation/protein-translation-test.arr index 2bfe5fd..efbe2db 100644 --- a/exercises/practice/protein-translation/protein-translation-test.arr +++ b/exercises/practice/protein-translation/protein-translation-test.arr @@ -164,9 +164,9 @@ fun stop-in-middle-of-six-codons(): end end -fun non-existent-codon(): - check "Non-existing codon can't translate": - proteins("AAA") raises "Invalid codon" +fun sequence-of-two-non-stop-codons(): + check "Sequence of two non-STOP codons does not translate to a STOP codon": + proteins("AUGAUG") is [list: "Methionine", "Methionine"] end end @@ -223,7 +223,7 @@ data TestRun: test(run, active) end test(stop-at-end-of-three-codons, false), test(stop-in-middle-of-three-codons, false), test(stop-in-middle-of-six-codons, false), - test(non-existent-codon, false), + test(sequence-of-two-non-stop-codons, false), test(unknown-codon, false), test(incomplete-sequence, false), test(incomplete-sequence-with-stop, false) diff --git a/exercises/practice/protein-translation/protein-translation.arr b/exercises/practice/protein-translation/protein-translation.arr index 1a25807..afd5cff 100644 --- a/exercises/practice/protein-translation/protein-translation.arr +++ b/exercises/practice/protein-translation/protein-translation.arr @@ -2,6 +2,54 @@ use context essentials2020 # Don't delete this line when using Pyret on Exercism provide: proteins end +include string-dict +import lists as L + +codon_mappings = [mutable-string-dict: + "AUG", "Methionine", + "UUU", "Phenylalanine", + "UUC", "Phenylalanine", + "UUA", "Leucine", + "UUG", "Leucine", + "UCU", "Serine", + "UCC", "Serine", + "UCA", "Serine", + "UCG", "Serine", + "UAU", "Tyrosine", + "UAC", "Tyrosine", + "UGU", "Cysteine", + "UGC", "Cysteine", + "UGG", "Tryptophan", + "UAA", "STOP", + "UAG", "STOP", + "UGA", "STOP"] + fun proteins(strand): - raise("Please implement the proteins function") + proteins-recursive(string-explode(strand), [list: ]) +end + +fun proteins-recursive(current, acc): + ask: + | current.length() == 0 then: acc + | current.length() < 3 then: raise("Invalid codon") + | otherwise: + block: + codon = current.take(3).join-str("") + protein = translate-codon(codon) + ask: + | protein == "STOP" then: acc + | otherwise: + rest = split-at(3, current).suffix + L.append([list: protein], proteins-recursive(rest, acc)) + end + end + end +end + + +fun translate-codon(codon): + cases(Option) codon_mappings.get-now(codon): + | some(a) => a + | none => raise("Invalid codon") + end end