Skip to content

Commit 41bb8f5

Browse files
ricemeryabo64
authored andcommitted
Add protein-translation exercise (#356)
* Add protein-translation exercise * Fix duplicate assertions in test suite
1 parent 08ef3a5 commit 41bb8f5

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@
236236
"Structural equality"
237237
]
238238
},
239+
{
240+
"slug":"protein-translation",
241+
"difficulty":3,
242+
"topics":[
243+
"Strings",
244+
"Transforming",
245+
"Sequences"
246+
]
247+
},
239248
{
240249
"slug":"matrix",
241250
"difficulty":4,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
scalaVersion := "2.12.1"
2+
3+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import scala.annotation.tailrec
2+
3+
object ProteinTranslation {
4+
5+
private val translations = Map(
6+
"AUG" -> "Methionine",
7+
"UUU" -> "Phenylalanine",
8+
"UUC" -> "Phenylalanine",
9+
"UUA" -> "Leucine",
10+
"UUG" -> "Leucine",
11+
"UCU" -> "Serine",
12+
"UCC" -> "Serine",
13+
"UCA" -> "Serine",
14+
"UCG" -> "Serine",
15+
"UAU" -> "Tyrosine",
16+
"UAC" -> "Tyrosine",
17+
"UGU" -> "Cysteine",
18+
"UGC" -> "Cysteine",
19+
"UGG" -> "Tryptophan",
20+
"UAA" -> "STOP",
21+
"UAG" -> "STOP",
22+
"UGA" -> "STOP")
23+
24+
/**
25+
* Note that this solution ignores invalid Codons.
26+
*/
27+
def translate(input: String): Seq[String] = {
28+
@tailrec
29+
def translate_internal(acc: Seq[String], strs: Seq[String]): Seq[String] = {
30+
strs match {
31+
case x::xs => translations get x match {
32+
case Some(codon) => if ("STOP".equals(codon)) acc.reverse else translate_internal(codon +: acc, xs)
33+
case _ => translate_internal(acc, xs)
34+
}
35+
case _ => acc.reverse
36+
}
37+
}
38+
39+
translate_internal(Seq(), input.grouped(3).toList)
40+
}
41+
}

exercises/protein-translation/src/main/scala/.keep

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import org.scalatest.{FunSuite, Matchers}
2+
3+
class ProteinTranslationTest extends FunSuite with Matchers {
4+
test("Identifies methionine codon") {
5+
ProteinTranslation.translate("AUG") should be(Seq("Methionine"))
6+
}
7+
8+
test("Identifies phenylalanine codons") {
9+
pending
10+
ProteinTranslation.translate("UUU") should be(Seq("Phenylalanine"))
11+
ProteinTranslation.translate("UUC") should be(Seq("Phenylalanine"))
12+
}
13+
14+
test("Identifies leucine codons") {
15+
pending
16+
ProteinTranslation.translate("UUA") should be(Seq("Leucine"))
17+
ProteinTranslation.translate("UUG") should be(Seq("Leucine"))
18+
}
19+
20+
test("Identifies serine codons") {
21+
pending
22+
ProteinTranslation.translate("UCU") should be(Seq("Serine"))
23+
ProteinTranslation.translate("UCC") should be(Seq("Serine"))
24+
ProteinTranslation.translate("UCA") should be(Seq("Serine"))
25+
ProteinTranslation.translate("UCG") should be(Seq("Serine"))
26+
}
27+
28+
test("Identifies tyrosine codons") {
29+
pending
30+
ProteinTranslation.translate("UAU") should be(Seq("Tyrosine"))
31+
ProteinTranslation.translate("UAC") should be(Seq("Tyrosine"))
32+
}
33+
34+
test("Identifies cysteine codons") {
35+
pending
36+
ProteinTranslation.translate("UGU") should be(Seq("Cysteine"))
37+
ProteinTranslation.translate("UGC") should be(Seq("Cysteine"))
38+
}
39+
40+
test("Identifies tryptophan codons") {
41+
pending
42+
ProteinTranslation.translate("UGG") should be(Seq("Tryptophan"))
43+
}
44+
45+
test("Translate RNA strand into correct protein") {
46+
pending
47+
ProteinTranslation.translate("AUGUUUUGG") should be(Seq("Methionine", "Phenylalanine", "Tryptophan"))
48+
}
49+
50+
test("Stops translation if stop codon is present") {
51+
pending
52+
ProteinTranslation.translate("AUGUUUUAA") should be(Seq("Methionine", "Phenylalanine"))
53+
}
54+
55+
test("Stops translation of longest strand") {
56+
pending
57+
ProteinTranslation.translate("UGGUGUUAUUAAUGGUUU") should be(Seq("Tryptophan", "Cysteine", "Tyrosine"))
58+
}
59+
}

0 commit comments

Comments
 (0)