Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def binary_search(array, element_ts):
- is_sorted = lambda x: all([el>x[i-1] for i, el in enumerate(x[1:], 1)])
- if len(array) < 1000 and not is_sorted(array):
- array = sorted(array)
- def recurse(first, last):
- mid = (first + last) // 2
- if array[mid] < element_ts:
- return recurse(mid+1, last)
- elif array[mid] > element_ts:
- return recurse(first, mid-1)
- elif array[mid] == element_ts:
- return mid + 1
- elif first > last:
- return -1
- else:
- raise
- return recurse(0, len(array)-1)
- if __name__ == '__main__':
- _ = input()
- array = list(map(int, input().split()))
- queries_num = int(input())
- for i in range(queries_num):
- query = int(input())
- print(binary_search(array, query))
Add Comment
Please, Sign In to add comment