Menu

Python Problem Problem 2 1 Already Need Write 3 Lines Codes Current Codes Q43887851

Python problem:(Problem #2, I did #1 already) (only need towrite 3 lines of codes)

[] class Queue (object): def __init_(self): self.queue = [] def _repr_(self): Defining a _repr__ function will enable us tois an element and n is the Heres the smart implementation of a counting queue that we saw during class. It stores pairs (2Problem 1: implement _len__ for a counting queue For this exercise, you will implement the _len__ method for the CountingQueuProblem 2 During lecture, we implemented an iterator for the Queue class using the generator technique. For this exercise, yo

My Current codes:

[] def countingqueue_iter(self): Iterates through all the elements of the queue, without removing them. for i in range (len

[] class Queue (object): def __init_(self): self.queue = [] def _repr_(self): “Defining a _repr__ function will enable us to print the queue contents, and facilitate debugging.””” return repr(self.queue) # Good enough. def add(self, x): self.queue.append(x) def get (self): # This is the only difference compared to the stack above. return self.queue.pop() if len(self.queue) > O else None def isempty(self): return len(self.queue) == 0 [ ] 9 = Queue) print(q.get() q.add(‘a’) q.add(‘b’) print(q.get() print(q.get() print(q.get() None None Python experts might note that, for a queue, we would do better by using the collections.degue class rather than the list class, to make the 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): a=0 for i in self.queue: a+=i[1] return a # This is a way to add a method to a class once the class # has already been defined. CountingQueue. __len__ = counting queue_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) Problem 2 During lecture, we implemented an iterator for the Queue class using the generator technique. For this exercise, you will use that same technique to implement an iterator for CountingQueue. Note: this can be done elegantly in 3 lines of code. [] def countingqueue_iter(self): “*”Iterates through all the elements of the queue, without removing them.” # YOUR CODE HERE raise NotImplementedError() # This is a way to add a method to a class once the class # has already been defined. CountingQueue. _iter__ = countingqueue_iter [] ### Tests for ‘CountingQueue. _iter_ q = CountingQueue () for i in range(10): q.add(‘a’) q.add(‘b’) for i in range (3): q.add(‘c’, count=2) 11 = [x for x in q] 12 = [] while not q. isempty(): 12.append(q.get) assert_equal(11, 12) [] def countingqueue_iter(self): “Iterates through all the elements of the queue, without removing them.” for i in range (len(self.queue)): yield self.queue[i] # This is a way to add a method to a class once the class # has already been defined. CountingQueue. _iter_ = countingqueue_iter CountingQueue. _len_=countingqueue_len [] ### Tests for CountingQueue. _iter_” 9 = CountingQueue for i in range (10): q.add(‘a’) q.add(‘b’) for i in range(3): q.add(‘c’, count=2) 11 = [x for x in q] 12 = [] while not a.isempty : 12.append(q.get ) assert_equal(11, 12) AssertionError Traceback (most recent call last) <ipython-input-12-9783e38a6615> in <module>() 10 while not q.isempty(): 11 12.append(q.get) —> 12 assert_equal(11, 12) – 3 frames /usr/lib/python3.6/unittest/case.py in fail(self, msg) 668 def fail(self, msg=None): 669 “Fail immediately, with the given message.” –> 670 raise self.failureException(msg) 671 672 def assertFalse (self, expr, msg=None): AssertionError: Lists differ: [(‘a’, 10), (‘b’, 1), (‘c’, 6)] != [‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘[38 chars] ‘c’] First differing element 0: (‘a’, 10) Second list contains 14 additional elements. First extra element 3: – [(‘a’, 10), (‘b’, 1), (‘C’, 6)] + [‘a’, + ‘a’, + ‘a’, Show transcribed image text [] class Queue (object): def __init_(self): self.queue = [] def _repr_(self): “Defining a _repr__ function will enable us to print the queue contents, and facilitate debugging.””” return repr(self.queue) # Good enough. def add(self, x): self.queue.append(x) def get (self): # This is the only difference compared to the stack above. return self.queue.pop() if len(self.queue) > O else None def isempty(self): return len(self.queue) == 0 [ ] 9 = Queue) print(q.get() q.add(‘a’) q.add(‘b’) print(q.get() print(q.get() print(q.get() None None Python experts might note that, for a queue, we would do better by using the collections.degue class rather than the list class, to make the
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): a=0 for i in self.queue: a+=i[1] return a # This is a way to add a method to a class once the class # has already been defined. CountingQueue. __len__ = counting queue_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)
Problem 2 During lecture, we implemented an iterator for the Queue class using the generator technique. For this exercise, you will use that same technique to implement an iterator for CountingQueue. Note: this can be done elegantly in 3 lines of code. [] def countingqueue_iter(self): “*”Iterates through all the elements of the queue, without removing them.” # YOUR CODE HERE raise NotImplementedError() # This is a way to add a method to a class once the class # has already been defined. CountingQueue. _iter__ = countingqueue_iter [] ### Tests for ‘CountingQueue. _iter_ q = CountingQueue () for i in range(10): q.add(‘a’) q.add(‘b’) for i in range (3): q.add(‘c’, count=2) 11 = [x for x in q] 12 = [] while not q. isempty(): 12.append(q.get) assert_equal(11, 12)
[] def countingqueue_iter(self): “Iterates through all the elements of the queue, without removing them.” for i in range (len(self.queue)): yield self.queue[i] # This is a way to add a method to a class once the class # has already been defined. CountingQueue. _iter_ = countingqueue_iter CountingQueue. _len_=countingqueue_len [] ### Tests for CountingQueue. _iter_” 9 = CountingQueue for i in range (10): q.add(‘a’) q.add(‘b’) for i in range(3): q.add(‘c’, count=2) 11 = [x for x in q] 12 = [] while not a.isempty : 12.append(q.get ) assert_equal(11, 12) AssertionError Traceback (most recent call last) in () 10 while not q.isempty(): 11 12.append(q.get) —> 12 assert_equal(11, 12) – 3 frames /usr/lib/python3.6/unittest/case.py in fail(self, msg) 668 def fail(self, msg=None): 669 “Fail immediately, with the given message.” –> 670 raise self.failureException(msg) 671 672 def assertFalse (self, expr, msg=None): AssertionError: Lists differ: [(‘a’, 10), (‘b’, 1), (‘c’, 6)] != [‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘[38 chars] ‘c’] First differing element 0: (‘a’, 10) Second list contains 14 additional elements. First extra element 3: – [(‘a’, 10), (‘b’, 1), (‘C’, 6)] + [‘a’, + ‘a’, + ‘a’,

Expert Answer


Answer to Python problem:(Problem #2, I did #1 already) (only need to write 3 lines of codes) My Current codes:…

OR