Skip to content

Commit 26f1f7e

Browse files
committed
add search range algorithm
1 parent 1f2bb52 commit 26f1f7e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

arrays/search_range.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
search_range([5, 7, 7, 8, 8, 8, 10], 8) => [3, 5]
3+
search_range([5, 7, 7, 8, 8, 8, 10], 11) => [None, None]
4+
complexity: O(n)
5+
"""
6+
7+
from typing import List
8+
9+
10+
def search_range(array: List[int], target: int) -> tuple:
11+
sorted_array = sorted(array)
12+
start_index = None
13+
end_index = None
14+
found = False
15+
for index, item in enumerate(sorted_array):
16+
if item == target and found is False:
17+
found = True
18+
start_index = index
19+
if item != target and found is True and end_index is None:
20+
end_index = index - 1
21+
return sorted_array, target, [start_index, end_index]
22+
23+
24+
if __name__ == "__main__":
25+
print(search_range([5, 7, 7, 8, 8, 8, 10], 8))
26+
print(search_range([5, 7, 7, 8, 8, 8, 10], 11))

0 commit comments

Comments
 (0)