Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Problem: is an element and n is the Here's the smart implementation of a counting queue that we saw during class. It stores pairs

Python Problem:

image text in transcribedimage text in transcribed

is an element and n is the Here's the "smart" implementation of a counting queue that we saw during class. It stores pairs (2,n), where count of the number of occurrences of 2. [1] 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() return x else: self.queue[@] = (x, 5 - 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 Problem 1: implement _len__ for a counting queue For this exercise, you will implement the __len__ method for the CountingQueue class defined above This should require no more than 4 lines of code. def countingqueue_len(self): "*"Returns the number of elements in the queue."" # YOUR CODE HERE raise NotImplementedError() # This is a way to add a method to a class once the class # has already been defined. CountingQueue. __len__ = countingqueue_len ] ### Tests for `_len__ from nose.tools import assert_equal q = CountingQueue () for i in range(10): q.add('a') q.add('b') for i in range (3): q.add('c', count=2) assert_equal(len(9), 17)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions

Question

Describe how light travels through the various parts of the eye.

Answered: 1 week ago