Question: # Note: Changing any part of the pre - implemented methods ( besides adding # # default parameters ) will cause the Gradescope tests to
# Note: Changing any part of the preimplemented methods besides adding #
# default parameters will cause the Gradescope tests to fail. #
from staticarray import StaticArray
class QueueExceptionException:
Custom exception to be used by Queue class.
DO NOT CHANGE THIS METHOD IN ANY WAY
pass
class Queue:
def initself None:
Initialize new queue based on Static Array.
DO NOT CHANGE THIS METHOD IN ANY WAY
self.sa StaticArray
self.front
self.back
self.currentsize
def strself str:
Override string method to provide more readable output.
DO NOT CHANGE THIS METHOD IN ANY WAY
size self.currentsize
out "QUEUE: strsize elements
frontindex self.front
for in rangesize :
out strselfsafrontindex
frontindex self.incrementfrontindex
if size :
out strselfsafrontindex
return out
def isemptyself bool:
Return True if the queue is empty, False otherwise.
DO NOT CHANGE THIS METHOD IN ANY WAY
return self.currentsize
def sizeself int:
Return number of elements currently in the queue.
DO NOT CHANGE THIS METHOD IN ANY WAY
return self.currentsize
def printunderlyingsaself None:
Print underlying StaticArray. Used for testing purposes.
DO NOT CHANGE THIS METHOD IN ANY WAY
printselfsa
def incrementself index: int int:
Move index to next position.
DO NOT CHANGE THIS METHOD IN ANY WAY
# employ wraparound if needed
index
if index self.salength:
index
return index
# #
def enqueueself value: object None:
TODO: Write this implementation
pass
def dequeueself object:
TODO: Write this implementation
pass
def frontself object:
TODO: Write this implementation
pass
# The method below is optional, but recommended, to implement. #
# You may alter it in any way you see fit. #
def doublequeueself None:
TODO: Write this implementation
pass
# BASIC TESTING
if namemain:
print
# Basic functionality tests #
print
# enqueue
q Queue
printq
for value in :
qenqueuevalue
printq
print
# dequeue
q Queue
for value in :
qenqueuevalue
printq
for in rangeqsize:
try:
printqdequeue
except Exception as e:
printNo elements in queue", typee
for value in :
qenqueuevalue
printq
qprintunderlyingsa
print
# front
q Queue
printq
for value in ABCD:
try:
printqfront
except Exception as e:
printNo elements in queue", typee
qenqueuevalue
printq
print
# Circular buffer tests: #
def actionandprint
header: str action: callable, values: queue: Queue None:
Print header, perform action,
then print queue and its underlying storage.
printheader
if values:
for value in values:
actionvalue
else:
action
printqueue
queue.printunderlyingsa
print
q Queue
# actionandprint# Enqueue: qenqueue, q
# Calling the actionandprint function declared two lines above,
# would be equivalent to following lines of code:
print# Enqueue:
testcase
for value in testcase:
qenqueuevalue
printq
qprintunderlyingsa
print
actionandprint# Dequeue a value", qdequeue, q
actionandprint# Enqueue: qenqueue, q
actionandprint# Enqueue: qenqueue, q
print# Dequeue until empty"
while not qisempty:
qdequeue
printq
qprintunderlyingsa
print
actionandprint# Enqueue: qenqueue, q
actionandprint# Enqueue: qenqueue, q
actionandprint# Enqueue: qenqueue,
q
actionandprint# Enqueue: qenqueue, q
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
