Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rotate_by_pos(lst, pos):
- n = len(lst)
- return lst[n - pos: n] + lst[0:n - pos]
- def binary_search(lst, el):
- first, last = 0, len(lst) - 1
- while first <= last:
- midpoint = (first + last) // 2
- if lst[midpoint] == el:
- return midpoint
- else:
- if lst[midpoint] > el >= lst[first]:
- last = midpoint - 1
- else:
- first = midpoint + 1
- return -1
- if __name__ == "__main__":
- arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- assert (binary_search(arr, 4) == 3)
- assert (binary_search(rotate_by_pos(arr, 1), 4) == 4)
- assert (binary_search(rotate_by_pos(arr, 2), 4) == 5)
- assert (binary_search(rotate_by_pos(arr, 6), 4) == 9)
- assert (binary_search(rotate_by_pos(arr, 7), 4) == 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement