-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch_range.py
68 lines (51 loc) · 1.58 KB
/
search_range.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Given a sorted array of integers, find the starting and ending position
of a given target value.
"""
def search_last_occurrence(arr, target):
first = 0
last = len(arr) - 1
found = -1
while first <= last:
mid = (first + last) / 2
if arr[mid] == target:
found = mid
first = mid + 1
if arr[mid] < target:
first = mid + 1
if arr[mid] > target:
last = mid - 1
return found
def search_first_occurrence(arr, target):
first = 0
last = len(arr) - 1
found = -1
while first <= last:
mid = (first + last) / 2
if arr[mid] == target:
found = mid
last = mid - 1
if arr[mid] < target:
first = mid + 1
if arr[mid] > target:
last = mid - 1
return found
def search_range(arr, target):
left = search_first_occurrence(arr, target)
right = search_last_occurrence(arr, target)
return [left, right]
def test_first_search_occurrence():
arr = [5, 7, 7, 7, 7, 7, 8, 8, 10]
assert search_first_occurrence(arr, 7) == 1
assert search_first_occurrence(arr, 8) == 6
def test_last_search_occurrence():
arr = [5, 7, 7, 7, 7, 7, 8, 8, 10]
assert search_last_occurrence(arr, 7) == 5
assert search_last_occurrence(arr, 8) == 7
def test_search_range():
arr = [5, 7, 7, 8, 8, 10]
assert search_range(arr, 4) == [-1, -1]
assert search_range(arr, 5) == [0, 0]
assert search_range(arr, 7) == [1, 2]
assert search_range(arr, 8) == [3, 4]
assert search_range(arr, 10) == [5, 5]