Created
July 8, 2011 09:42
-
-
Save Chris927/1071464 to your computer and use it in GitHub Desktop.
chop implementation, see http://codekata.pragprog.com/2007/01/kata_two_karate.html
This file contains hidden or 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(v, values) | |
| ix = values.length / 2 | |
| m = values[ ix ] | |
| return ix if v == m | |
| return nil if values.length < 2 | |
| if v > m | |
| ix + 1 + chop(v, values[ix+1..-1]) rescue nil | |
| else | |
| chop(v, values[0..ix-1]) rescue nil | |
| end | |
| end | |
| a = [ 1, 4, 7, 13, 54, 88, 102 ] | |
| puts chop(0, a) | |
| puts chop(1, a) | |
| puts chop(4, a) | |
| puts chop(7, a) | |
| puts chop(14, a) | |
| puts chop(88, a) | |
| puts chop(102, a) | |
| puts chop(103, a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment