Advertisement
Falexom

Untitled

Jan 20th, 2023
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. def shift(lst, steps):
  2.     if steps < 0:
  3.         steps = abs(steps)
  4.         for i in range(steps):
  5.             lst.append(lst.pop(0))
  6.     else:
  7.         for i in range(steps):
  8.             lst.insert(0, lst.pop())
  9.  
  10.  
  11. def binary_search(lst, value):
  12.     print(lst)
  13.     print(value)
  14.     mid = len(lst) // 2
  15.     low = 0
  16.     high = len(lst) - 1
  17.     while lst[mid] != value and low <= high:
  18.         if value > lst[mid]:
  19.             low = mid + 1
  20.         else:
  21.             high = mid - 1
  22.         mid = (low + high) // 2
  23.  
  24.     if low > high:
  25.         return "No value"
  26.     else:
  27.         return mid
  28.  
  29.  
  30. def catch(lst, counter):
  31.     if lst[0] <= lst[-1]:
  32.         return counter
  33.  
  34.     else:
  35.         return catch(lst[0:len(lst) - 1], counter + 1)
  36.  
  37.  
  38. def weird_search(lst):
  39.     count = catch(lst, 0)
  40.     data = len(lst)-count-1
  41.     if count == 0:
  42.         return len(lst)-1
  43.     else:
  44.         return data
  45.  
  46.  
  47. def my_search(lst, value):
  48.     left = lst[0]
  49.     right = lst[-1]
  50.     mid = len(lst) // 2
  51.     if left < right:
  52.         output = binary_search(lst, value)
  53.         return output
  54.  
  55.     foo = weird_search(lst[:mid])
  56.     bar = weird_search(lst[mid-1:])
  57.     if lst[foo] > lst[bar+mid-1]:
  58.         return foo
  59.     else:
  60.         return bar+mid-1
  61.  
  62.  
  63. def LinearSearch(lys, element):
  64.     for i in range(len(lys)):
  65.         if lys[i] == element:
  66.             return i
  67.     return -1
  68.  
  69.  
  70. a = []
  71.  
  72. for i in range(1, 100):
  73.     a.append(i)
  74.  
  75. for i in range(1, 100):
  76.     a = sorted(a)
  77.     point = a[-1]
  78.  
  79.     shift(a, i)
  80.     print(a)
  81.  
  82.     print(my_search(a, point), LinearSearch(a, point))
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement