Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def pivot_index_mo3(values, left, right):
- """
- Returns the index of the item that is the median of the left, right and
- middle value in the list. The return value should normally be
- either left, right or middle.
- If there are only two items in the range, ie, if right==left+1
- then return the index of the first item as there are only two items
- to find the median of, so we can't get a middle index...
- If there is only one item in the range then also simply
- return the left index, ie, if left==right then return left.
- >>> print(pivot_index_mo3([0,1,2],0,2))
- 1
- >>> pivot_index_mo3([2,1,0],0,2)
- 1
- >>> pivot_index_mo3([1,2,3],0,2)
- 1
- >>> pivot_index_mo3([3,2,1],0,2)
- 1
- >>> pivot_index_mo3([3,5,1],0,2)
- 0
- >>> pivot_index_mo3([1,5,3],0,2)
- 2
- >>> pivot_index_mo3([1,2],0,1)
- 0
- >>> pivot_index_mo3([3,1],0,1)
- 0
- >>> pivot_index_mo3([1,2],1,1)
- 1
- >>> x = [1,1,3]
- >>> i = pivot_index_mo3(x,0,2)
- >>> x[i]
- 1
- >>> y = [1,3,1]
- >>> i = pivot_index_mo3(y,0,2)
- >>> y[i]
- 1
- >>> z = [3,1,1]
- >>> i = pivot_index_mo3(z,0,2)
- >>> z[i]
- 1
- >>> xx = [1,3]
- >>> i = pivot_index_mo3(xx,0,1)
- >>> xx[i]
- 1
- >>> pivot_index_mo3([1,2,2,5,2,8,10],0,6)
- 3
- >>> pivot_index_mo3([1,6,0,5,9,8,10],0,4)
- 0
- """
- middle = (left + right) // 2
- # ---start student section---
- if values[left] < values[middle]:
- if values[middle] < values[right]:
- return middle
- elif values[left] < values[right]:
- return right
- else:
- return left
- elif values[left] < values[right]:
- return left
- elif values[middle] < values[right]:
- return right
- else:
- return middle
- # ===end student section===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement