USE PYTHON!!!!!!!!!!!!!!! nce. write a class i ntOueue that stores a queue of i
ID: 3697844 • Letter: U
Question
USE PYTHON!!!!!!!!!!!!!!!
nce. write a class i ntOueue that stores a queue of i nt·s. A queue ems on one end and removes/dequeues from the other. You MUST om list. Errors will result if any code attempts to enqueue anything 2. Container/ tainer/Inheritance. Writ ueues it inherit/subclass from 1 other than an intQueue. int, or, if code attempts to assign to a location in the middle of the An intQueue can be constructed with a given list, if none is given it defaults to the em list: >>>intQueue) intQueue ([1) >>> intQueue (13,4,51) intQueue[[3, 4, 5)) The method enqueue adds to the back, dequeue removes from the front and returns the item qintQueue (15,61) >>> q.enqueue (7) >>>q.enqueue (8) intQueue (I5, 6, 7, 8]) >>> q.dequeue )--5 True >>> q.dequeue )--6 True intQueue ((7, 81) Any attempt to add anything but an int raises an error. This can happen in enqueue or the constructor >>>iq intQueue ) >>>iq.enqueue ( 5.5 Traceback (most recent call last): NotIntError: 5.5 is not an int >>> iq.enqueue( 'hello' Traceback (most recent call last): NotIntError: hello is not an int >>>iq.enqueue( [) Traceback (most recent call last): NotIntError: [ is not an int >>> intQueue ( 13, 4 . 55,5)) Traceback (most recent cal1 last): NotIntError: 4.55 is not an int ...continues on next page...Explanation / Answer
class Queuehere(object):
def __init__(self):
self.headhere = 0
self.tailhere = 0
self.n = 0
self.l = []
// enqueue function to add alement at tail
def enqueuefunc(self, elementhere):
self.l.append(elementhere)
self.tailhere += 1
self.n += 1
// dequeue function to remove the element from head from list
def dequeuefunc(self):
if self.n == 0: return None
e = self.l[self.headhere]
self.headhere += 1
self.n -= 1
return e
def size(self):
return self.n
def elements(self):
return [self.l[i] for i in xrange(self.headhere, self.tailhere)]
class LinkedQueuehere(object):
def __init__(self):
import linkedlist
self.l = linkedlist.LinkedList()
def enqueue(self, elementhere):
self.l.append(elementhere)
def dequeue(self):
return self.l.front()
def size(self):
return self.l.size()
def elements(self):
return [x for x in self.l.elements()]
if __name__ == "__main__":
q = Queuehere()
assert None == q.dequeuefunc()
q.enqueuefunc(5)
q.enqueuefunc(6)
q.enqueuefunc(7)
assert 1 == q.dequeuefunc()
assert 2 == q.dequeuefunc()
assert 3 == q.dequeuefunc()
q.enqueuefunc(5)
q.enqueuefunc(6)
q.enqueuefunc(7)
q.enqueuefunc(8)
q.enqueuefunc(8)
assert 1 == q.dequeuefunc()
assert 2 == q.dequeuefunc()
assert 3 == q.dequeuefunc()
assert 4 == q.dequeuefunc()
assert 5 == q.dequeuefunc()
z = LinkedQueuehere()
assert None == z.dequeuefunc()
z.enqueuefunc(1)
z.enqueuefunc(2)
z.enqueuefunc(3)
assert 1 == z.dequeuefunc()
assert 2 == z.dequeuefunc()
assert 3 == z.dequeuefunc()
z.enqueuefunc(1)
z.enqueuefunc(2)
z.enqueuefunc(3)
z.enqueuefunc(4)
z.enqueuefunc(5)
assert 1 == z.dequeuefunc()
assert 2 == z.dequeuefunc()
assert 3 == z.dequeuefunc()
assert 4 == z.dequeuefunc()
assert 5 == z.dequeuefunc()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.