-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path02-search-corrected.py
More file actions
39 lines (30 loc) · 1.05 KB
/
02-search-corrected.py
File metadata and controls
39 lines (30 loc) · 1.05 KB
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
def search(needle, haystack):
left = 0
right = len(haystack) - 1
while left <= right:
middle = left + (right - left) // 2
middle_element = haystack[middle]
if middle_element == needle:
return middle
elif middle_element < needle:
left = middle + 1
else:
right = middle - 1
raise ValueError("Value not in haystack")
def test_search():
assert search(2, [1, 2, 3, 4]) == 1, \
'found needle somewhere in the haystack'
def test_search_first_element():
assert search(1, [1, 2, 3, 4]) == 0, \
'search first element'
def test_search_last_element():
assert search(4, [1, 2, 3, 4]) == 3, \
'search last element'
def test_exception_not_found():
from pytest import raises
with raises(ValueError, message="left out of bound"):
search(-1, [1, 2, 3, 4])
with raises(ValueError, message="right out of bound"):
search(5, [1, 2, 3, 4])
with raises(ValueError, message="not found in middle"):
search(2, [1, 3, 4])