## AppylPye 2 years ago Following is a solution for Mechanical MOOC MIT OCW 6.189 Homework 3, Exercise 3.6 - Your First Class, in which we need to create a Queue. http://ideone.com/WCxNBy The program inserts and removes "random" integers to test the Queue. The code was written for Python 3. Another strategy for creating the Queue class could be to store the values in a list instead of in a private __Node class. Does anyone else have a solution for this exercise?

1. AlbionsRefuge

Ha, I tried to make the queue a list at first but then I couldn't decide how to control the FIFO bit so I made it a string instead. This will need revisited for sure! http://ideone.com/58GkH1

2. AppylPye

Yeah, I had thought of using a list, which would have been simpler, but chose instead to create Nodes and maintain pointers to the front and back of the Queue. With a list implementation, append(item) could be used to add the item to back of the Queue. To remove an item from the front of the Queue, item = theList.pop([0]) would remove the item at the 0th position in the list, and return it, assigning it to item. Another means of doing this could be item = theList[0], and then theList = theList[1:]. We should consider the (non)efficiency of these operations, though.

3. AlbionsRefuge

Arg, well I did start with the pop() and append() and the slice but then I started thinking about something that was mentioned somewhere? (ha!) about the order of the items might not be as expected. I've gone and done some reading and I think I've straighten my brain out on that, but I can't remember yet what exactly was being talked about that got me on the wrong track.

4. AppylPye

With a list, the order of the items would be reliable. But you may have read that in a dictionary, there is not an inherent ordering to the items. In other words, if you assign the following to a dictionary ... d = {1: "one", 2: "two", 3: "three", 4: "four"} ... and then ... print d ... the items will not necessarily print in the same order in which they were assigned or added to the dictionary, nor can they be relied upon to print according to any ordering that is implied by the keys.

5. AlbionsRefuge

That's it - yes that was what I read. Thanks!

Find more explanations on OpenStudy