-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_broken_array.py
39 lines (32 loc) · 1.1 KB
/
search_broken_array.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
# 68547154
from typing import Tuple
def read_input() -> Tuple[int, list]:
"""Read input."""
_, element = input(), int(input())
list_nubers = list(map(int, input().strip().split()))
return element, list_nubers
def broken_search(array: list, search_element: int) -> int:
"""The function returns the index of the desired element,
if there is one in the array (numbering from zero).
If the element is not found, the function should return -1.
"""
start = 0
end = len(array) - 1
while start <= end:
middle = (start + end) // 2
if array[middle] == search_element:
return middle
if array[start] <= array[middle]:
if array[start] <= search_element < array[middle]:
end = middle - 1
else:
start = middle + 1
else:
if array[middle] < search_element <= array[end]:
start = middle + 1
else:
end = middle - 1
return -1
if __name__ == '__main__':
search_value, array = read_input()
print(broken_search(array, search_value))