Please write the function twice. One for each scanario and such that each one sa
ID: 3880272 • Letter: P
Question
Please write the function twice. One for each scanario and such that each one satisfies each of the test codes. Must be done in python.
1:
2:
The class car is defined as below class car: def-init-(self, brand, reg-no): self._brand brand self.- reg-no = reg-no def-str-(self) return (str(self._brand)++str(self._reg_no) Write a circular queue to provide line-up function of the cars. The skeleton of class Queuecircular is shown below: class Queuecircular: def-init-(self, n): Initialize the list of object, cars, with size n def enqueue(self, item) Enqueue def dequeue(self) Deque and return the dequeued car def printfront(self) Print the information of the front car def printback(self) Print the information of the back car def printall(self) Print the information of all the cars on the queue (start from the front) For example Test Result q Queue circular(2) car1 car( BMW", "AB1234" car2 = car("Porsche", "GF1455") q.enqueue(car1) q. enqueue(car2) q.printfrontO q.printback) BMW:AB1234 Porsche: GF1455 q = Queuecircular(2) carl-car("BMW" , "AB1234") car2 = car("Porsche", "GF1455") q. enqueue(car1) q. enqueue(car2) q.printfront) q.printbackO q.printallO BMW:AB1234 Porsche:GF1455 BMW:AB1234 Porsche:GF1455Explanation / Answer
1.
Code:
##Car class defination
class car:
def __init__(self, brand, reg_no):
self.__brand = brand
self.__reg_no = reg_no
def __str__(self):
return (str(self.__brand) + ":" + str(self.__reg_no))
## Circular queue defination.
class Queuecircular:
def __init__(self, n):
self.queue = [None] * n
self.head = 0
self.tail = 0
self.maxSize = n
#Adding elements to the queue
def enqueue(self,data):
## Queue is empty.
if self.queue[self.tail] == None and self.tail == self.head:
self.queue[self.tail] = data
else :
self.tail = (self.tail + 1) % self.maxSize
self.queue[self.tail] = data
return True
##removing the element from the queue.
def dequeue(self):
data = self.queue[self.head]
self.queue[self.head] = None
if self.head != self.tail:
self.head = (self.head + 1) % self.maxSize
return data
#Calculating the size of the queue
def size(self):
if self.queue[self.tail] == None and self.tail == self.head:
return 0
if self.tail>=self.head:
return (self.tail-self.head + 1)
return (self.maxSize - (self.head-self.tail) + 1)
## Printing the front of queue.
def printfront(self):
print self.queue[self.head]
## Printing the back.
def printback(self):
print self.queue[self.tail]
##Printing all
def printall(self):
head = self.head
size = self.size()
while (size > 0):
print self.queue[head]
head = (head + 1 ) % self.maxSize
size = size -1
q = Queuecircular(2)
car1 = car("BMW", "ABCD1234")
car2 = car("porsche", "GF1455")
q.enqueue(car1)
q.enqueue(car2)
q.printfront()
q.printback()
q.printall()
Output screenshot:
$ python main.py
BMW:ABCD1234
porsche:GF1455
BMW:ABCD1234
porsche:GF1455
------------------------------------------------------------------------------------------------------------------------------------------
2.
Code:
class car:
def __init__(self, brand, reg_no):
self.__brand = brand
self.__reg_no = reg_no
def __str__(self):
return (str(self.__brand) + ":" + str(self.__reg_no))
## Circular queue defination.
class Queuecircular:
def __init__(self, n):
self.queue = [None] * n
self.head = 0
self.tail = 0
self.maxSize = n
#Adding elements to the queue
def enqueue(self,data):
if self.size() == self.maxSize:
print "Queue is Full!"
return False
## Queue is empty.
if self.queue[self.tail] == None and self.tail == self.head:
self.queue[self.tail] = data
else :
self.tail = (self.tail + 1) % self.maxSize
self.queue[self.tail] = data
return True
##removing the element from the queue.
def dequeue(self):
if self.size() == 0:
print "Queue is empty!"
return "Queue empty"
data = self.queue[self.head]
self.queue[self.head] = None
if self.head != self.tail:
self.head = (self.head + 1) % self.maxSize
return data
#Calculating the size of the queue
def size(self):
if self.queue[self.tail] == None and self.tail == self.head:
return 0
if self.tail>=self.head:
return (self.tail-self.head + 1)
return (self.maxSize - (self.head-self.tail) + 1)
## Printing the front of queue.
def printfront(self):
print self.queue[self.head]
## Printing the back.
def printback(self):
print self.queue[self.tail]
##Printing all
def printall(self):
head = self.head
size = self.size()
while (size > 0):
print self.queue[head]
head = (head + 1 ) % self.maxSize
size = size -1
q = Queuecircular(2)
car1 = car("BMW", "ABCD1234")
q.enqueue(car1)
q.printfront()
q.printback()
q.printall()
q.dequeue()
q.dequeue()
Output screenshot:
$ python main.py
BMW:ABCD1234
BMW:ABCD1234
BMW:ABCD1234
Queue is empty!