rubyで2分探索

class Search
  def binary(ary,v)
    point = ary.length / 2
    while true
      if v < ary[point]
        if point == 0
          point = nil
          break
        end
        point = point / 2
      elsif v > ary[point]
        if point == ary.length - 1
          point = nil
          break
        end
        point = (point + ary.length) / 2
      elsif v == ary[point]
        break
      end
    end
    point
  end
end

require "test/unit"
require_relative "./search.rb"

class TestSearch < Test::Unit::TestCase
  def setup
    @obj = Search.new
  end

  def test_binary
    assert_equal(0,@obj.binary([0,1,2,3,4,5,6,7,8,9],0))
    assert_equal(9,@obj.binary([0,1,2,3,4,5,6,7,8,9],9))
    assert_equal(nil,@obj.binary([0,1,2,3,4,5,6,7,8,9],10))
    assert_equal(nil,@obj.binary([0,1,2,3,4,5,6,7,8,9],-1))
  end
end

Started
.
Finished in 0.000366 seconds.
-------------------------------------------------------------------------------
1 tests, 4 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
-------------------------------------------------------------------------------
2732.24 tests/s, 10928.96 assertions/s

今日は2分探索をやった。androidのソフトウェアキーボードでコード書いてて、効率が上がるかと思ってbluetoothキーボード買ったんだけど、まさかのUS配列。AmazonChoiceだから購入したんだけど、US配列なんかChoiceすんなよな。返品しようか迷ったけど、返品の手続きのこと考えると、めんどくさくて。めんどくさいから返品はしないけど、使うかっていったら微妙だな。

あまり使う予定のないbluetoothキーボード、電池代、スタンド代含めて3500円。高い買い物したよ。