Using the BinaryHeap class, implement a new class called PriorityQueue. Your Pri
ID: 3567834 • Letter: U
Question
Using the BinaryHeap class, implement a new class called PriorityQueue. Your PriorityQueue class should implement the constructor, plus the enqueue and dequeue methods.
this should print something like below but the numbers in each list are shuffled, so may be different:
test inserts from: [1]
test inserts from: [2, 1]
test inserts from: [4, 3, 1, 5, 2]
test inserts from: [6, 8, 10, 9, 3, 2, 4, 5, 1, 7]
test inserts from: [11, 1, 17, 4, 14, 6, 13, 18, 2, 7, 16, 15, 12, 8, 10, 19, 5, 9, 20, 3]
ALL PASSED
Explanation / Answer
from Queue import PriorityQueue class MyPriorityQueue(PriorityQueue): def __init__(self): PriorityQueue.__init__(self) self.counter = 0 def put(self, item, priority): PriorityQueue.put(self, (priority, self.counter, item)) self.counter += 1 def get(self, *args, **kwargs): _, _, item = PriorityQueue.get(self, *args, **kwargs) return item queue = MyPriorityQueue() queue.put('item2', 1) queue.put('item1', 1) print queue.get() print queue.get()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.