fork(16) download
  1. # -*- coding: utf-8 -*-
  2. # MIT OCW 6.189 Homework 3
  3. # Exercise 3.6 – Your First Class
  4. # Mechanical MOOC
  5. # Judy Young
  6. # July 26, 2013
  7.  
  8. # This exercise, plus the remaining written questions (3.10 - 3.11) will be the start
  9. # of our journey into object-oriented programming; we suggest you do these written
  10. # exercises before tackling this question.
  11.  
  12. # For this exercise, you will be coding your very first class, a Queue class.
  13. # Queues are a fundamental computer science data structure. A queue is basically
  14. # like a line at Disneyland - you can add elements to a queue, and they maintain
  15. # a specific order. When you want to get something off the end of a queue, you get
  16. # the item that has been in there the longest (this is known as ‘first-in-first-out’,
  17. # or FIFO). You can read up on queues at Wikipedia if you’d like to learn more.
  18.  
  19. # Create a new file called queue.py to make your Queue class.
  20. # In your Queue class, you will need three methods:
  21. # • init : to initialize your Queue (think: how will you store the queue’s elements?
  22. # You’ll need to initialize an appropriate object attribute in this method)
  23. # • insert: inserts one element in your Queue
  24. # • remove: removes one element from your Queue and returns it.
  25. # If the queue is empty, return a message that says it is empty.
  26.  
  27. class Queue(object):
  28. def __init__(self):
  29. self.elements = ""
  30.  
  31.  
  32. def insert(self, element):
  33. self.elements += element
  34. print "...", element, "added"
  35. # return "\n", element, "has been added to the end of this queue"
  36.  
  37. def remove(self):
  38. if len(self.elements) == 0:
  39. return "\nThis Queue is empty"
  40. else:
  41. element_to_remove = self.elements[0]
  42. self.elements = self.elements[1:]
  43. return "\n" + element_to_remove + ", the first/oldest item in this Queue has been removed"
  44.  
  45.  
  46. # let's see what it looks like to add and remove elements
  47.  
  48. my_queue = Queue()
  49.  
  50. print "my_queue =", my_queue.elements
  51. print my_queue.remove()
  52.  
  53.  
  54. print "\nAdding some elements ..."
  55. my_queue.insert("a")
  56. my_queue.insert("b")
  57. my_queue.insert("c")
  58. my_queue.insert("d")
  59. print "my_queue =", my_queue.elements
  60.  
  61. print my_queue.remove()
  62. print "my_queue =", my_queue.elements
  63.  
  64. print "\nAdding some elements ..."
  65. my_queue.insert("e")
  66. my_queue.insert("f")
  67. my_queue.insert("g")
  68. print "my_queue =", my_queue.elements
  69.  
  70. print my_queue.remove()
  71. print "my_queue =", my_queue.elements
  72.  
  73. print my_queue.remove()
  74. print my_queue.remove()
  75. print my_queue.remove()
  76. print my_queue.remove()
  77. print my_queue.remove()
  78. print my_queue.remove()
  79.  
  80.  
Success #stdin #stdout 0.01s 7728KB
stdin
Standard input is empty
stdout
my_queue = 

This Queue is empty

Adding some elements ...
... a added
... b added
... c added
... d added
my_queue = abcd

a, the first/oldest item in this Queue has been removed
my_queue = bcd

Adding some elements ...
... e added
... f added
... g added
my_queue = bcdefg

b, the first/oldest item in this Queue has been removed
my_queue = cdefg

c, the first/oldest item in this Queue has been removed

d, the first/oldest item in this Queue has been removed

e, the first/oldest item in this Queue has been removed

f, the first/oldest item in this Queue has been removed

g, the first/oldest item in this Queue has been removed

This Queue is empty