Created
June 13, 2011 22:09
-
-
Save jrsconfitto/1023844 to your computer and use it in GitHub Desktop.
Code Kata Two solutions
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
class Chop | |
def chop(target, values) | |
size = values.size | |
if size == 0 | |
-1 | |
elsif size == 1 | |
(values[0] == target ? 0 : -1) | |
else | |
if target < values[size/2] | |
chop(target, values[0..size/2-1]) | |
else | |
result = chop(target, values[size/2..size]) | |
(result != -1 ? size/2 + result : -1) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are my attempts at CodeKata two solutions.
-The first is iterative
-The second is recursive
-The third (4th commit) is recursive and a little different