USE PYTHON!!!!!!!!!!!!!!! nce. write a class i ntOueue that stores a queue of i
ID: 3697929 • 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 intQueue():
def __init__(self,l=[]):
for i in l:
if(type(i) is not int):
raise NotIntError(str(i)+" is not an int")
self.l=l
def __repr__(self):
return 'intQueue('+str(self.l)+')'
def enqueue(self,n):
if(type(n) is not int):
raise NotIntError(str(n)+" is not an int")
self.l.append(n)
def dequeue(self):
self.num = self.l[0]
self.l = self.l[1:]
return self.num
def __setitem__(self,index,value):
raise CuttingError("no cutting")
class NotIntError(Exception):
def __init__(self,message):
self.message=message
class CuttingError(Exception):
def __init__(self,message):
self.message=message
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.