-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20200728_rotated_index.py
More file actions
85 lines (67 loc) · 2.67 KB
/
20200728_rotated_index.py
File metadata and controls
85 lines (67 loc) · 2.67 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
2020-07-28
[from dailycodingproblem.com #58]
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.
'''
# COMMENTS
# This solution requires O(n/2 + 1) time in the worse case scenario.
# It checks only every other index, and compares them to the element whose
# index is desired.
# The last values lower/larger than the desired element, and their indices,
# are continually updated to help determine whether the desired element is
# between two neighboring checked indices
def rotated_index(array, elem):
# Values initialized to -1 or elem to avoid clumsy None checking
last_lower = -1
last_lower_value = elem
last_larger = -1
last_larger_value = elem
for i in range(0, len(array), 2):
num = array[i]
if num == elem:
return i
elif num < elem:
if i - last_lower == 2 and num < last_lower_value:
# If elem is largest in array
return i - 1
last_lower = i
last_lower_value = num
elif num > elem:
if i - last_lower == 2:
# If elem is in order between neighboring numbers
return i - 1
if last_larger_value > num and i - last_larger == 2:
# If elem is smallest in array
return i - 1
if last_larger_value < num:
last_larger = i
last_larger_value = num
return len(array) - 1
'''
# TESTS
rotated_index([8, 10, 13, 18, 25, 2], 8) == 0
rotated_index([2, 8, 10, 13, 18, 25], 8) == 1
rotated_index([25, 2, 8, 10, 13, 18], 8) == 2
rotated_index([18, 25, 2, 8, 10, 13], 8) == 3
rotated_index([13, 18, 25, 2, 8, 10], 8) == 4
rotated_index([10, 13, 18, 25, 2, 8], 8) == 5
# Desired element is smallest element
rotated_index([2, 8, 10, 13, 18, 25], 2) == 0
rotated_index([25, 2, 8, 10, 13, 18], 2) == 1
rotated_index([18, 25, 2, 8, 10, 13], 2) == 2
rotated_index([13, 18, 25, 2, 8, 10], 2) == 3
rotated_index([10, 13, 18, 25, 2, 8], 2) == 4
rotated_index([8, 10, 13, 18, 25, 2], 2) == 5
# Desired element is largest element
rotated_index([25, 2, 8, 10, 13, 18], 25) == 0
rotated_index([18, 25, 2, 8, 10, 13], 25) == 1
rotated_index([13, 18, 25, 2, 8, 10], 25) == 2
rotated_index([10, 13, 18, 25, 2, 8], 25) == 3
rotated_index([8, 10, 13, 18, 25, 2], 25) == 4
rotated_index([2, 8, 10, 13, 18, 25], 25) == 5
'''