def test_binary_search_with(): """Test for a value that exists""" assert bs([4, 8, 15, 16, 23, 42], 15) == 2
def test_binary_search_without(): """Test for a value that exists""" assert bs([11, 22, 33, 44, 55, 66, 77], 90) == -1
def search(_list, target): low = 1 while low < len(_list) and _list[low] <= target: low *= 2 return bs(_list[low // 2:], target) + low // 2
def invalid_data(self): with self.assertRaises(TypeError): bs('Invalid Text', 5)
def ranges(self): self.assertEqual(bs(input, -5), -1) # less than min element self.assertEqual(bs(input, 15), -1) # greater than max element self.assertEqual(bs(input, 4.5), -1) # between min and max, but not found
def weird_data(self): self.assertEqual(bs([5, 1.5, 10], 1.5), 2) # Float #1 self.assertEqual(bs([1.5, 5, 10], 5), 1) # Float #2 self.assertEqual(bs([5, 10, inf], inf), 2) # Infinity self.assertEqual(bs([5, nan, 10], nan), 1) # NaN
def array_length(self): self.assertEqual(bs([], 1), -1) # length 0 self.assertEqual(bs([1], 1), 0) # length 1 self.assertEqual(bs([0, 1], 1), 1) # length 2 self.assertEqual(bs([0, 1], 1, 0, 0), -1) # value out of [min...max]