-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosestValue.py
More file actions
27 lines (25 loc) · 823 Bytes
/
Copy pathClosestValue.py
File metadata and controls
27 lines (25 loc) · 823 Bytes
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
def closestValue(values, target):
left = 0
right = len(values)-1
while left <= right:
guess = (left+right)//2
if values[guess] == target:
return target
elif values[guess] > target:
right = guess - 1
else:
left = guess + 1
if values[left] - target < target - values[right]:
return values[left]
else:
return values[right]
if __name__ == '__main__':
lst = []
nums = ['1','2','3','4','5','6','7','8','9','0']
while True:
temp = input("Give sorted array of values from which to find closest value, type END to finish passing values: ")
if temp == 'end' or 'END':
break
lst.append(int(temp))
target = int(input("Give target value: "))
print(closestValue(lst, target))