Skip to content

Instantly share code, notes, and snippets.

@rapha
Created October 14, 2009 12: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 rapha/210041 to your computer and use it in GitHub Desktop.
Save rapha/210041 to your computer and use it in GitHub Desktop.
Karate Chop Kata in Scala
def chop(needle : Int, haystack : Array[Int]) : Int = {
if (haystack.length == 0) return -1
val midPoint = haystack.length / 2
val midValue = haystack(midPoint)
if (midValue == needle)
midPoint
else if (needle < midValue)
chop(needle, haystack.slice(0, midPoint))
else
chop(needle, haystack.slice(midPoint + 1, haystack.length)) match {
case -1 => -1
case index => midPoint + 1 + index
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment