# 6.S189 Sample Solutions - Exercise 8.3 # queue_sol.py # ********** Exercise 8.3 ********** class Queue(object): def __init__(self): "Initializes 1 attribute: a list to keep track of the queue's elements" self.qlist = [] def insert(self, element): "Adds an element to the attribute list using append" self.qlist.append(element) def remove(self): "Removess an element from the attribute list" # Check if the list is empty; if so then display a message if self.qlist == []: print 'The queue is empty' else: # Otherwise we want to remove the first element from the list # and return that to the user. element = self.qlist[0] self.qlist.remove(element) return element # Can also do this in 1 line: # return self.qlist.pop(0) # Test for Exercise 8.3 queue = Queue() queue.insert(5) queue.insert(6.5) queue.insert('Bear') print queue.remove() == 5 print queue.remove() == 6.5 print queue.remove() == 'Bear' queue.remove()