A little help please #mooc, #mmooc #seq15oct12

One of our sequence 1 (Seq15October12) participants is struggling with an assignment and so far not getting an assist on OpenStudy.  Anyone want to step up and help?  Just answer in the comments here.

Dear MOOC:

I am trying to complete MIT homework “Exercise 3.6 – Your First
Class” for Gentle Python Week 6. The code below is as far as I’ve gotten.
My problem is two-fold:
1) I don’t know what I’m appending to, if .append is the correct method
for this exercise
2) If .append is the correct method, I’m unclear on how queue and Queue
interact with each when .append is applied.
Perhaps by helping me with Q1 the 2nd question will answer itself.
I know the exercise also requires me to do a “remove,” which I assume
means write a def remove(self) and include a .pop method. But if I can
first learn how to append I assume I can fairly easily add a .pop method.
– – – – – –
class Queue(object):
def __init__(self, num):
num.self = num
def insert(self):
queue = []
queue.append(num)
queue = Queue()
queue.insert(5)

Advertisements

About MOOC-E

The Mechanical MOOC, international man (or machine) of mystery. (Photo credit: Tinkerbots http://bit.ly/P029IR)
This entry was posted in Content questions, Seq15Oct12 and tagged , , . Bookmark the permalink.

5 Responses to A little help please #mooc, #mmooc #seq15oct12

  1. nickrusnov says:

    Presumably they are asking you to implement a Queue class, and then demonstrate it working. So the lines above queue=Queue() are the starting code for the Queue class, then queue = Queue() makes a variable queue assigned to an instance of the Queue class, and then queue.insert(5) adds 5 to the queue. Presumably you’ll want an instance variable on the Queue class which stores your queue, and change the prototype of append to accept a number, something like:

    class Queue(object):
    def __init__(self):
    self.contents = list() # changed from ‘queue to avoid confusion
    def insert(self, num):
    self.contents.append(num)
    def remove(self):
    … # I’ll let you fill in this code, basically oldest item (FIFO) from the contents and return

    Now if you run your code:

    myqueue = Queue()
    myqueue.insert(5)

    myqueue’s contents property will contain a 5.

    • nickrusnov says:

      Of course the spacing got all messed up. Here’s the same code on a pastebin:

      • JM says:

        I’m trying to do first in-first out but my queue.remove doesn’t pop and print “5” (though if I uncomment print self.contents I can see that “5” has been popped). Why isn’t the pop method in queue.remove() working as I would like it to?
        class Queue(object):
        def __init__(self):
        self.contents = list()
        def insert(self, num):
        self.contents.append(num)
        def remove(self):
        if self.contents == []:
        print “The queue is empty”
        else:
        self.contents.pop(0)
        ##print self.contents

        queue = Queue()
        queue.insert(5)
        queue.insert(6)
        queue.remove() #Goal is to print “5”.

  2. JM says:

    @nickrusnov: Thanks much. On the road right now but will give it a try later.

  3. JM says:

    Sorry, never mind. I realized that if I write “print self.contents.pop(0)” instead of just “self.contents.pop(0)” I accomplish my goal. I forgot that unlike in the Python Shell pop doesn’t print output without a return or print command.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s