-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding058.py
59 lines (43 loc) · 1.55 KB
/
dailycoding058.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
"""
This problem was asked by Amazon.
An sorted array of integers was rotated an unknown number of times.
Given such an array, find the index of the element in the array in
faster than linear time. If the element doesn't exist in the array, return null.
For example, given the array [13, 18, 25, 2, 8, 10] and the element 8,
return 4 (the index of 8 in the array).
You can assume all the integers in the array are unique.
Solution:
Use rotated binary search to find the index in O(logn)
"""
def index_search(arr, val, start, end):
"""Performs recursive rotated binary search to find the index of val
"""
mid = start + (end - start) // 2
if arr[mid] == val:
return mid
elif arr[mid] > arr[start]: # left is sorted
if arr[start] <= val and val <= arr[mid]:
return index_search(arr, val, start, mid - 1)
else:
return index_search(arr, val, mid + 1, end)
else: # right must be sorted otherwise
if arr[mid] <= val and val <= arr[end]:
return index_search(arr, val, mid + 1, end)
else:
return index_search(arr, val, start, mid - 1)
def argval(arr, val):
"""Main function for rotated binary search
"""
return index_search(arr, val, 0, len(arr) - 1)
def main():
tests = [
[13, 18, 25, 2, 8, 10],
[10, 13, 18, 25, 2, 8],
[18, 25, 2, 8, 10, 13]
]
if all(all(argval(t, num) == i for i, num in enumerate(t)) for t in tests):
print("Passed")
else:
print("Failed")
if __name__ == '__main__':
main()