Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. '''Rearrange list around the pivot such that elements less than the pivot A[i]
  2. are placed to the left and elements gretaer than the pivot to the right of
  3. the pivot. Book Elements of programming interviews in Py (2018) p.44
  4. Hint: Think about the partition step in quicksort. Duch national flag
  5. partitioning method..
  6. '''
  7.  
  8. A = [4,1,2,11,2,3,5,7,3,4,1,6]
  9.  
  10. def rearrange(pivot):
  11.    
  12.     right = (i for i in A if i > A[pivot])
  13.     left = (i for i in A if i < A[pivot])
  14.     middle = (i for i in A if i == A[pivot])
  15.        
  16.     return (left, middle, right)
  17.  
  18. r = rearrange(2)
  19. for partition in r:
  20.     for i in r[partition]:
  21.         print(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement