Skip to content

Instantly share code, notes, and snippets.

@koriroys
Created August 17, 2012 22:44
Show Gist options
  • Save koriroys/3383309 to your computer and use it in GitHub Desktop.
Save koriroys/3383309 to your computer and use it in GitHub Desktop.
def chop(target, array, range_slice = 0)
return chop(target, array, 0..(array.size - 1)) if range_slice == 0
first = range_slice.first
last = range_slice.last
size = last - first + 1
if size >= 2
midpoint = (last - first) / 2 + first
if array[midpoint] == target
midpoint
elsif array[midpoint] > target
chop(target, array, left_slice(range_slice))
else
chop(target, array, right_slice(range_slice))
end
elsif last == first
array[first] == target ? first : -1
else
-1
end
end
def left_slice(range)
size = range.last - range.first + 1
if size % 2 == 0
upper_limit = size / 2 - 1 + range.first
else
upper_limit = size / 2 - 1 + range.first
end
range.first..upper_limit
end
def right_slice(range)
size = range.last - range.first + 1
lower_limit = size / 2 + range.first
lower_limit..range.last
end
require 'chop'
describe "chop" do
context "empty array" do
it "fails to find the target specified" do
expect(chop 3, []).to eq(-1)
end
end
context "single element array" do
it "fails if target not in array" do
expect(chop 3, [1]).to eq(-1)
end
it "returns target index if target found in array" do
expect(chop 1, [1]).to eq(0)
end
end
context "three element array" do
it "returns target index if target is found" do
expect(chop 1, [1, 3, 5]).to eq(0)
expect(chop 3, [1, 3, 5]).to eq(1)
expect(chop 5, [1, 3, 5]).to eq(2)
end
it "fails when the target is not found" do
expect(chop 0, [1, 3, 5]).to eq(-1)
expect(chop 2, [1, 3, 5]).to eq(-1)
expect(chop 4, [1, 3, 5]).to eq(-1)
expect(chop 6, [1, 3, 5]).to eq(-1)
end
end
context "four element array" do
it "returns target index if target is found" do
expect(chop 1, [1, 3, 5, 7]).to eq(0)
expect(chop 3, [1, 3, 5, 7]).to eq(1)
expect(chop 5, [1, 3, 5, 7]).to eq(2)
expect(chop 7, [1, 3, 5, 7]).to eq(3)
end
it "fails when the target is not found" do
expect(chop 0, [1, 3, 5, 7]).to eq(-1)
expect(chop 2, [1, 3, 5, 7]).to eq(-1)
expect(chop 4, [1, 3, 5, 7]).to eq(-1)
expect(chop 6, [1, 3, 5, 7]).to eq(-1)
expect(chop 8, [1, 3, 5, 7]).to eq(-1)
end
end
end
describe "left and right array slices" do
describe "#left_slice" do
context "returns the range for the left side of an array" do
specify "for even sized arrays" do
expect(left_slice 0..1).to eq(0..0)
expect(left_slice 1..2).to eq(1..1)
expect(left_slice 1..4).to eq(1..2)
expect(left_slice 4..9).to eq(4..6)
end
specify "for odd sized arrays" do
expect(left_slice 1..3).to eq(1..1)
expect(left_slice 1..5).to eq(1..2)
expect(left_slice 3..7).to eq(3..4)
end
end
end
describe "#right_slice" do
context "returns the right side of an array" do
specify "for even sized arrays" do
expect(right_slice 0..1).to eq(1..1)
expect(right_slice 1..2).to eq(2..2)
expect(right_slice 1..4).to eq(3..4)
end
specify "for odd sized arrays" do
expect(right_slice 1..3).to eq(2..3)
expect(right_slice 1..5).to eq(3..5)
end
end
end
end
source "http://rubygems.org"
gem "rspec"
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.2)
PLATFORMS
ruby
DEPENDENCIES
rspec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment