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.
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)Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.