Advertisement
furas

Python - heap, heapify, nsmallest

May 2nd, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. import heapq
  2. import random
  3.  
  4. data = [random.randint(0, 9) for x in range(10)]
  5.  
  6. print("A:", data)
  7. heapq.heapify(data)
  8. print("B:", data)
  9. data = heapq.nsmallest(10, data)
  10. print("C:", data)
  11.  
  12. print()
  13.  
  14. data = list(reversed(range(10)))
  15.  
  16. print("A:", data)
  17. heapq.heapify(data)
  18. print("B:", data)
  19. data = heapq.nsmallest(10, data)
  20. print("C:", data)
  21.  
  22. '''
  23. A: [5, 6, 5, 3, 4, 5, 4, 0, 8, 0]
  24. B: [0, 0, 4, 3, 4, 5, 5, 5, 8, 6]
  25. C: [0, 0, 3, 4, 4, 5, 5, 5, 6, 8]
  26.  
  27. A: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  28. B: [0, 1, 3, 2, 5, 4, 7, 9, 6, 8]
  29. C: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  30. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement