kAldown

shit

Nov 7th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. from random import shuffle
  2.  
  3. #s1 = [
  4. #        4,
  5. #        1,
  6. #        2,
  7. #        3,
  8. #        5,
  9. #        8,
  10. #        6,
  11. #        7,
  12. #]
  13.  
  14. s1 = range(15)
  15. shuffle(s1)
  16.  
  17.  
  18. def split_stacks(s1, tmp=None):
  19.     stash = []
  20.     tmp = tmp or []
  21.     while s1:
  22.         #print('source', s1)
  23.         #print('stash', stash)
  24.         #print('tmp', tmp)
  25.  
  26.         value = s1.pop()
  27.         # top
  28.         top = stash[-1] if stash else None
  29.         if top is None or value < top:
  30.             stash.append(value)
  31.         else:
  32.             tmp.append(value)
  33.     return stash, tmp
  34.  
  35.  
  36.  
  37. ordered = []
  38. tmp = []
  39. stash = s1
  40. while True:
  41.     stash, tmp = split_stacks(stash, tmp=tmp)
  42.  
  43.  
  44.     if len(stash) == 1:
  45.         ordered.append(stash.pop())
  46.         if not tmp:
  47.             break
  48.         stash = tmp
  49.         tmp = []
  50.  
  51.  
  52. print('done')
  53. print(ordered)
Advertisement
Add Comment
Please, Sign In to add comment