Question: class CountingQueue (object): def __init__(self): self.queue = [] def _repr_(self): return repr(self.queue) def add(self, x, count=1): # If the element is the same as the

 class CountingQueue (object): def __init__(self): self.queue = [] def _repr_(self): returnrepr(self.queue) def add(self, x, count=1): # If the element is the sameas the last element, we simply # increment the count. This assumes

class CountingQueue (object): def __init__(self): self.queue = [] def _repr_(self): return repr(self.queue) def add(self, x, count=1): # If the element is the same as the last element, we simply # increment the count. This assumes we can test equality of # elements. if len(self.queue) > 0: xx, CC = self.queue (-1] if xx == x: self.queue (-1] (xx, CC + count) else: self.queue.append((x, count)) else: self.queue = [(x, count)] def get (self): if len(self.queue) == 0: return None X, C = self.queue [0] if c == 1: self.queue.pop(0) return x else: self.queue [0] = (x, C - 1) return x = def isempty(self): # Since the count of an element is never 0, we can just check # whether the queue is empty. return len(self.queue) == 0 def counting_queue_in(self, el): # YOUR CODE HERE CountingQueue. _in__ = counting_queue_in = # 0 points. Simple cases. q = CountingQueue () assert "cat" not in a q.add("cat", count=2) assert "cat" in q assert "dog" not in a q.add("dog") assert "cat" in a assert "dog" in q q.get() assert "cat" in q assert "dog" in a q.get() assert "cat" not in a assert "dog" in a q.get() assert "cat" not in a assert "dog" not in a [ ] # 0 points. Behaves the same as Queue. elements = range(5) for k in range (100): 90 = Queue () q1 = CountingQueue () for - in range (20): el = random.choice(elements) q0.add(el) q1.add(el) for x in elements: assert (x in qo) (x in q1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!