-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_for_Range.py
More file actions
84 lines (59 loc) · 1.94 KB
/
Search_for_Range.py
File metadata and controls
84 lines (59 loc) · 1.94 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
"""
usage:
find_range([1,1,1,2],2)
find_range([1,3,3,3,4],3)
find_range([1,1,1,2,4,4],4)
find_range([1,1,1,1,1,1],1)
find_range([1,3,4],4)
"""
def find_range(lst, target):
"""
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums is a non-decreasing array.
-109 <= target <= 109
"""
if len(lst) > 105:
raise ValueError("Length > 105")
rangecheck = all( elem >= 0 and elem <= 109 for elem in lst)
if not rangecheck:
raise ValueError("element value out of bounds")
min = lst[0]
nondec = True
for item in range(1, len(lst)):
if lst[item] >= min:
min = lst[item]
continue
else:
nondec = False
break
if nondec == False:
raise ValueError("Error not non Dec")
if target > 109 or target < -109:
raise ValueError("Target is out of bounds")
left, right = bin_search(lst, target, 0, len(lst)-1, False)
print ([left, right])
def bin_search(lst, target, left, right, to_print=False):
if len(lst) == 0:
return
if to_print:
print (f"left = {left}; right = {right}; lst = {lst};")
mid = left+right//2
if to_print:
print (f"mid={mid}")
if target == lst[mid]:
left = right = mid
#get left end
while left > 0 and lst[left-1] == target:
left -=1
#get right end
while right < len(lst)-1 and lst[right+1] == target:
right +=1
if to_print:
print (f"Inner: {[left,right]}")
return left, right
if target < lst[mid]:
return bin_search(lst, target, left, mid-1)
if target > lst[mid]:
return bin_search(lst, target, mid, right)
return left, right