Created
October 14, 2009 12:55
-
-
Save rapha/210041 to your computer and use it in GitHub Desktop.
Karate Chop Kata in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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