daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top