batasrki (owner)

Revisions

gist: 239274 Download_button fork
public
Public Clone URL: git://gist.github.com/239274.git
Embed All Files: show embed
spec_for_code_kata_2.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
it "should time how long it takes to find a number in a set of 100" do
  # when uniq! is executed the number of items in the array is roughly halved, so I need to double the seed
  seed = []
  (1..200).each { |num| seed << rand(num)}
  seed.uniq!.sort!
  index_found = search(81, seed)
  x.report("elight's:") { bchop(81, seed).should == index_found}
end
 
#linear search function to compare against
def search(num, list)
  return -1 if list.length == 0 || (list.length == 1 && list[0] != num)
   #index = list.index(num)
   list.each_with_index do |value, index|
     return index if value == num
   end
   return -1
end