OOP
Estimated time
20-45 minutes
Level of difficulty
Easy/Medium
1)Scenario
As you already know, a stack is a data structure realizing the so-called LIFO (Last In - First Out) model. It's easy and you've already grown perfectly accustomed to it.
Let's taste something new now. A queue is a data model characterized by the term FIFO: First In - Fist Out. Note: a regular queue (line) you know from shops or post offices works exactly in the same way - a customer who came first is served first too.
Your task is to implement the Queue class with two basic operations:
put(element), which puts an element at end of the queue;get(), which takes an element from the front of the queue and returns it as the result (the queue cannot be empty to successfully perform it.)
Follow the hints:
- use a list as your storage (just like we did in stack)
put()should append elements to the beginning of the list, whileget()should remove the elements from the list's end;- define a new exception named
QueueError(choose an exception to derive it from) and raise it whenget()tries to operate on an empty list.
Complete the code we've provided in the editor. Run it to check whether its output is similar to ours.
Expected output
1
dog
False
Queue error
2)Scenario
Your task is to slightly extend the Queue class' capabilities. We want it to have a parameterless method that returns True if the queue is empty and False otherwise.
Complete the code we've provided in the editor. Run it to check whether it outputs a similar result to ours.
CODE:
class QueueError(IndexError):
pass
class Queue:
def __init__(self):
self.queue = []
def put(self,elem):
self.queue.insert(0,elem)
def get(self):
if len(self.queue) > 0:
elem = self.queue[-1]
del self.queue[-1]
return elem
else:
raise QueueError
class SuperQueue(Queue):
def isempty(self):
return len(self.queue) == 0
que = SuperQueue()
que.put(1)
que.put("dog")
que.put(False)
for i in range(4):
if not que.isempty():
print(que.get())
else:
print("Queue empty")
Comments
Post a Comment