Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Created July 17, 2013 14:45
Show Gist options
  • Save gclaramunt/6021242 to your computer and use it in GitHub Desktop.
Save gclaramunt/6021242 to your computer and use it in GitHub Desktop.
Monotonicity count
import scala.annotation.tailrec
/**
* User: gabriel
* Date: 7/17/13
*/
object Mono {
def findMonotonicityBreaks(items:Seq[Int]):Int={
@tailrec
def breaksRec(itms:Seq[Int],currMono:Option[Int], count:Int):Int=itms match {
case x::y::ys=> {
val newMono=x.compare(y)
val c=currMono.map(cm=>if (newMono != 0 && newMono != cm) 1 else 0).getOrElse(0)
breaksRec(y::ys,Some(newMono),count+c )
}
case _ => count
}
breaksRec(items,None,0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment