Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using python 3.4.1 Using the BinaryHeap class, implement a new class called Prio

ID: 3567940 • Letter: U

Question

using python 3.4.1

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()