Advertisement
makispaiktis

Greedy Algorithms - Heappop Example

May 29th, 2020 (edited)
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. from heapq import heappush
  2. from heapq import heappop
  3. # Heappop returns the element of the heap with the minimum value
  4.  
  5. heappush(h, (1, "ccc"))
  6. heappush(h, (2, "bbb"))
  7. heappush(h, (-1, "ccc"))
  8. # Στα pop θα πετάξει πρώτα το tuple με το μικρότερο πρώτο στοιχείο, αυτό με το -1
  9.  
  10. h = list()
  11. heappush(h, (1, "ccc"))
  12. heappush(h, (2, "bbb"))
  13. heappush(h, (-1, "ccc"))
  14. print(heappop(h))
  15. print(heappop(h))
  16. print(heappop(h))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement