Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Created on Thu Sep 25 20:17:57 2014
- # @author: Danilo de Jesus da Silva Bellini
- """
- Priority Queue in Python standard library.
- Using Queue/queue module, or heapq module directly.
- """
- from __future__ import print_function
- from heapq import heappush, heappop
- try:
- from Queue import PriorityQueue # Python 2
- except ImportError:
- from queue import PriorityQueue # Python 3
- print("Using the {} module!".format(PriorityQueue.__module__))
- q = PriorityQueue()
- for el in [5, 2, 7, 9, 3, 1]:
- q.put(el)
- print(q.get())
- print(q.get())
- q.put(4)
- print(q.get())
- q.put(14)
- q.put(2)
- print(q.get())
- print(q.get())
- q.put(13)
- while not q.empty():
- print(q.get())
- print()
- print("Using the heapq module!")
- pq = []
- for el in [5, 2, 7, 9, 3, 1]:
- heappush(pq, el)
- print(heappop(pq))
- print(heappop(pq))
- heappush(pq, 4)
- print(heappop(pq))
- heappush(pq, 14)
- heappush(pq, 2)
- print(heappop(pq))
- print(heappop(pq))
- heappush(pq, 13)
- while pq:
- print(heappop(pq))
Advertisement
Add Comment
Please, Sign In to add comment