Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. def pivot_index_mo3(values, left, right):
  2. """
  3. Returns the index of the item that is the median of the left, right and
  4. middle value in the list. The return value should normally be
  5. either left, right or middle.
  6. If there are only two items in the range, ie, if right==left+1
  7. then return the index of the first item as there are only two items
  8. to find the median of, so we can't get a middle index...
  9. If there is only one item in the range then also simply
  10. return the left index, ie, if left==right then return left.
  11.  
  12. >>> print(pivot_index_mo3([0,1,2],0,2))
  13. 1
  14. >>> pivot_index_mo3([2,1,0],0,2)
  15. 1
  16. >>> pivot_index_mo3([1,2,3],0,2)
  17. 1
  18. >>> pivot_index_mo3([3,2,1],0,2)
  19. 1
  20. >>> pivot_index_mo3([3,5,1],0,2)
  21. 0
  22. >>> pivot_index_mo3([1,5,3],0,2)
  23. 2
  24. >>> pivot_index_mo3([1,2],0,1)
  25. 0
  26. >>> pivot_index_mo3([3,1],0,1)
  27. 0
  28. >>> pivot_index_mo3([1,2],1,1)
  29. 1
  30. >>> x = [1,1,3]
  31. >>> i = pivot_index_mo3(x,0,2)
  32. >>> x[i]
  33. 1
  34. >>> y = [1,3,1]
  35. >>> i = pivot_index_mo3(y,0,2)
  36. >>> y[i]
  37. 1
  38. >>> z = [3,1,1]
  39. >>> i = pivot_index_mo3(z,0,2)
  40. >>> z[i]
  41. 1
  42. >>> xx = [1,3]
  43. >>> i = pivot_index_mo3(xx,0,1)
  44. >>> xx[i]
  45. 1
  46. >>> pivot_index_mo3([1,2,2,5,2,8,10],0,6)
  47. 3
  48. >>> pivot_index_mo3([1,6,0,5,9,8,10],0,4)
  49. 0
  50. """
  51.  
  52. middle = (left + right) // 2
  53. # ---start student section---
  54. if values[left] < values[middle]:
  55. if values[middle] < values[right]:
  56. return middle
  57. elif values[left] < values[right]:
  58. return right
  59. else:
  60. return left
  61. elif values[left] < values[right]:
  62. return left
  63. elif values[middle] < values[right]:
  64. return right
  65. else:
  66. return middle
  67. # ===end student section===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement