Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. def rotate_by_pos(lst, pos):
  2.     n = len(lst)
  3.     return lst[n - pos: n] + lst[0:n - pos]
  4.  
  5.  
  6. def binary_search(lst, el):
  7.     first, last = 0, len(lst) - 1
  8.     while first <= last:
  9.         midpoint = (first + last) // 2
  10.         if lst[midpoint] == el:
  11.             return midpoint
  12.         else:
  13.             if lst[midpoint] > el >= lst[first]:
  14.                 last = midpoint - 1
  15.             else:
  16.                 first = midpoint + 1
  17.     return -1
  18.  
  19.  
  20. if __name__ == "__main__":
  21.     arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  22.     assert (binary_search(arr, 4) == 3)
  23.     assert (binary_search(rotate_by_pos(arr, 1), 4) == 4)
  24.     assert (binary_search(rotate_by_pos(arr, 2), 4) == 5)
  25.     assert (binary_search(rotate_by_pos(arr, 6), 4) == 9)
  26.     assert (binary_search(rotate_by_pos(arr, 7), 4) == 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement