Challenge: I would like to find when logic changes from HIGH(1) to LOW (0) and vice versa.
Numeric examples:
Below is my functional code:
def get_state(value):
if value > 3.5:
return 1
if value < 2.5:
return 0
def find_state_changed(arr):
low_state = get_state(min(arr))
high_state = get_state(max(arr))
for up in arr:
if get_state(up) != low_state:
LowHigh = up
break
for low in arr[::-1]:
if get_state(low) != high_state:
HighLow = low
break
return LowHigh, HighLow
low = 0
high = 5
iteration = 100
step = (high - low)/iteration
arr = [round(i * step,2) for i in list(range(iteration+1))]
print(find_state_changed(arr))
Need help with:
One of my colleague mentioned it could be done with a binary search I have tried to integrate it into my code but miserably failed... If anyone knows, if it is even possible to use the binary search in this code or there is a more efficient way of doing it, please let me know.
Because your array is sorted you using binary search allow to pass from average O(n) to O(log(n)) which can be dramatic on large datasets.
You have to customize the binary search because you do not search an exact value.
For example you may do something like this:
def custom_binary_search(arr, value):
"""
return the position of value in arr if value in arr. Alse return the position of the nearest superior value.
:param arr:
:param value:
:return:
"""
_arr = arr[::]
left_ = 0
right_ = len(arr) - 1
while True: # possible infinite loops here
middle_i = int((right_ - left_) / 2)
middle_v = arr[middle_i]
if middle_v == value:
return middle_i
elif middle_v < value and arr[middle_i + 1] > value:
return middle_i + 1
elif middle_v < value:
right_ = middle_i
elif middle_v > value:
left_ = middle_i
else:
raise AssertionError("Should never reach this condition")