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

PYTHON Write a container class called PostitvePriorityQueue. The class should su

ID: 3881030 • Letter: P

Question

PYTHON

Write a container class called PostitvePriorityQueue. The class should support methods:

• Constructor: Initializes the queue • insert(): Takes a number as input and adds it to the container. Does not allwo negitive numbers and raises an error if a negative number attempted.

Write a container class called PostitvePriorityQueue. The class should support methods Constructor: Initializes the queue insert(): Takes a number as input and adds it to the container. Does not allwo negitive numbers and raises an error if a negative number attempted . min): Returns the smallest number in the container . removeMin): Removes the smallest number in the container removeMax(): Removes the largest number from the container isEmpty(): Returns True if container is empty, False otherwise . clear): remove all items from the container . . · If a negative number is added. A Custom exception is raised named "NegativeNumberError" The overloaded operator len() and an iterator should also be supported -PositivePriorityQueue (1 insert (10) >>> q.insert (20) a.insert (30) >naert(-30) Traceback (most recent cal11 1ast) g.insert (-30 242CSC 242 Dayweek 5Solutionscac242assi Fil. "C:User zokot DesktopCSC gns-solutions . py", . line 6, 1n insert raise NegativellumberError Negativetlumbertzzor .min >>g.removeMiN 1sEmpty print () 20 30 >>>len(a) 9S

Explanation / Answer

class NegativeNumberError(Exception): pass class PostitvePriorityQueue: def __init__(self): self.queue = [] def insert(self, n): if n < 0: raise NegativeNumberError else: self.queue.append(n) def min(self): return min(self.queue) def removeMin(self): self.queue.remove(self.min()) def removeMax(self): self.queue.remove(self.max()) def isEmpty(self): return len(self.queue) == 0 def clear(self): self.queue = [] def __len__(self): return len(self.queue) def __iter__(self): return self q = PostitvePriorityQueue() q.insert(10) q.insert(20) q.insert(30) q.min() q.removeMin() q.isEmpty() print(q.queue)