using python!!! 2. Container/inheritance. Write a class intQueue that stores a q
ID: 3697989 • Letter: U
Question
using python!!!
2. Container/inheritance. Write a class intQueue that stores a queue of int's . A queue adds/enqueues items on one end and removes/dequeues from the other. You MUST inherit/subclas other than an intQueue s from list. Errors will result if any code attempts to enqueue anything int, or, if code attempts to assign to a location in the middle of the An int list: Queue can be constructed with a given list, if none is given it defaults to the empty >intQueue ) intQueue ([) >>> intQueue ([3,4,5]) intQueue (13, 4, 5]) he method enqueue adds to the back, dequeue removes from the front and returns the item >>> q = intQueue ([ 5,6]) >>q.enqueue (7) >>>q.enqueue (8) intQueue ([5, 6, 7, 81) >>>q. dequeue )-5 True >>> q.dequeue()=-6 True intQueue (7, 81) Aany attenot to ad anh but an int raises an error. This can happen in enqueue or the constructor >>> iqintQueue) >>>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 cal1 last): NotIntError: [ is not an int >>intQueue (13, 4.55, 5]) Traceback (most recent call 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.