Sha256: 1f331ec904d634c22e69d80e7c577a8e4cc36dea9faa022c58ee44e3f7a3643d

Contents?: true

Size: 1.04 KB

Versions: 160

Compression:

Stored size: 1.04 KB

Contents

import scala.annotation.tailrec

object ProteinTranslation {

  private val translations = Map(
    "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")

  /**
    * Note that this solution ignores invalid Codons.
    */
  def translate(input: String): Seq[String] = {
    @tailrec
    def translate_internal(acc: Seq[String], strs: Seq[String]): Seq[String] = {
      strs match {
        case x::xs => translations get x match {
          case Some(codon) => if ("STOP".equals(codon)) acc.reverse else translate_internal(codon +: acc, xs)
          case _ => translate_internal(acc, xs)
        }
        case _ => acc.reverse
      }
    }

    translate_internal(Seq(), input.grouped(3).toList)
  }
}

Version data entries

160 entries across 160 versions & 1 rubygems

Version Path
trackler-2.2.1.90 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.89 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.88 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.87 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.86 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.85 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.84 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.83 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.82 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.81 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.80 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.79 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.78 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.77 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.76 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.75 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.74 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.73 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.72 tracks/scala/exercises/protein-translation/example.scala
trackler-2.2.1.71 tracks/scala/exercises/protein-translation/example.scala