Skip to content

Instantly share code, notes, and snippets.

@maasg
Created August 31, 2013 09:27
Show Gist options
  • Save maasg/6397175 to your computer and use it in GitHub Desktop.
Save maasg/6397175 to your computer and use it in GitHub Desktop.
object ATower {
def lengthEncode(l: List[Int]):List[Tuple2[Int,Int]] = l match {
case Nil => List()
case n::Nil => List((n,1))
case n::tail => val (repeats, rest) = tail.span(x=>x==n); (n,repeats.length+1) :: lengthEncode(rest)
}
def towers(l1: String, l2:String):(Int,Int) = {
val lines = l1.toInt
val data = l2.split(" ").map(_.toInt).sorted.toList
val lencoded = lengthEncode(data)
val max = lencoded.maxBy(_._2)._2
(max,lencoded.length)
}
towers("4","6 5 6 7") //> res0: (Int, Int) = (2,3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment