Skip to content

Instantly share code, notes, and snippets.

@Vervalle
Forked from tyrcho/ATGC.scala
Created May 23, 2013 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vervalle/5636665 to your computer and use it in GitHub Desktop.
Save Vervalle/5636665 to your computer and use it in GitHub Desktop.
import scala.util.Random
import scala.collection.immutable.SortedMap
object ATGC extends App {
def randomString(size: Int) = List.fill(size)(randomChar).mkString
def randomChar = "ATGC"(Random.nextInt(4))
def analyze(s: String): Map[String, Int] = {
val (_, _, res) = (s + "Z").tail.foldLeft((s.head, 1, SortedMap.empty[String, Int] withDefaultValue 0)) {
case ((last, count, map), current) if current == last => (last, count + 1, map)
case ((last, count, map), current) if current != last =>
val key = last.toString * count
val value = map(key) + 1
(current, 1, map.updated(key, value))
}
res
}
val s = randomString(10)
println(s)
println(analyze(s))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment