Commit 30c9d52
Changed files (1)
spec
kata
spec/kata/karate_chop_spec.rb
@@ -1,7 +1,6 @@
#http://codekata.com/kata/kata02-karate-chop/
require "spec_helper"
-
# Write a binary chop method that takes an integer search target and a sorted array of integers.
# It should return the integer index of the target in the array, or -1 if the target is not in the array.
# The signature will logically be:
@@ -31,6 +30,23 @@ describe "chop" do
end
end
+ def chop(target, items)
+ low = 0
+ high = items.size
+
+ while low < high
+ mid = low + (high-low)/2
+ if items[mid] == target
+ return mid
+ elsif target > items[mid]
+ low = mid + 1
+ else
+ high = mid
+ end
+ end
+ -1
+ end
+
def assert_equal(expected, actual)
expect(actual).to eql(expected)
end