-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
44 lines (36 loc) · 981 Bytes
/
binary_search.py
File metadata and controls
44 lines (36 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import unittest
def binary_search(target, data):
data.sort()
start = 0
end = len(data) - 1
while start <= end:
mid = (start + end) // 2
if data[mid] == target:
return mid
elif data[mid] < target:
start = mid + 1
else:
end = mid - 1
return None
class BinarySearchTest(unittest.TestCase):
def test_binary_search(self):
self.assertEqual(
binary_search(4, [1, 2, 3, 4, 5]), 3
)
self.assertEqual(
binary_search(4, [5, 4, 3, 2, 1]), 3
)
self.assertEqual(
binary_search(5, [5, 4, 3, 2, 1]), 4
)
self.assertEqual(
binary_search(1, [5, 4, 3, 2, 1]), 0
)
self.assertEqual(
binary_search(6, [1, 2, 3, 4, 5]), None
)
self.assertEqual(
binary_search(0, [5, 4, 3, 2, -1]), None
)
if __name__ == '__main__':
unittest.main()