-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch Insert Position.py
41 lines (32 loc) · 1.05 KB
/
Search Insert Position.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
#!/usr/bin/python
'''
Given a sorted array of distinct integers and a target value, return the index if the target is found.
If not, return the index where it would be if it were inserted in order.
'''
def main():
nums = list(map(int, input("Enter array: ").strip().split()))
target = int(input("Enter target number: "))
result = 0
final = 0
def binary(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary(arr, low, mid - 1, x)
else:
return binary(arr, mid + 1, high, x)
else:
return -1
result = binary(nums, 0, (len(nums)) - 1, target)
if result != -1:
print(result)
else:
nums.append(target)
nums.sort()
print(nums)
final = binary(nums, 0, (len(nums)) - 1, target)
print(final)
if __name__ == "__main__":
main()