fork download
  1. import random
  2.  
  3. def binSearch(arr,target):
  4. a=0
  5. b=len(arr)-1
  6. if target < arr[a] | target > arr[b]:
  7. return (-1,0)
  8. if target == arr[a]:
  9. return (0,1)
  10. if target == arr[b]:
  11. return (b,1)
  12. counter=0
  13. while(True):
  14. if (b-a)<=1:
  15. return (-1,counter)
  16. counter+=1
  17. c=(a+b)//2
  18. if target==arr[c]:
  19. return (c,counter)
  20. if arr[c]>target:
  21. b=c
  22. else:
  23. a=c
  24.  
  25. arr=[]
  26. for i in range(33):
  27. arr+=[int(100*random.random())]
  28. arr=sorted(arr)
  29.  
  30. a=int(100*random.random())
  31. print(str(arr))
  32. print(a)
  33. res=binSearch(arr,a)
  34. print(res)
  35.  
  36.  
  37.  
Success #stdin #stdout 0.01s 36944KB
stdin
Standard input is empty
stdout
[0, 0, 1, 1, 5, 7, 13, 15, 24, 26, 27, 28, 35, 37, 37, 44, 45, 48, 53, 58, 59, 60, 63, 64, 65, 66, 73, 79, 81, 83, 83, 94, 99]
53
(18, 4)