6 Replies - 1326 Views - Last Post: 04 March 2013 - 10:44 PM Rate Topic: -----

#1 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

How to count loops?

Posted 04 March 2013 - 09:50 PM

class Queue:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.insert(0,item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

def hotPotato(namelist, num):
    simqueue = Queue()
    for name in namelist:
        simqueue.enqueue(name)
        print(name)

    while simqueue.size() > 1:
        for i in range(num):
            simqueue.enqueue(simqueue.dequeue())
          

        simqueue.dequeue()

    return simqueue.dequeue()

print(hotPotato(["Bill","David","Susan","Jane","Kent","Brad"],7))



So I have this program and Im so confused.
when I enqueue the names my simqueue looks like this right:["Brad","Kent","Jane","Susan","David","Bill"], right?

when the for loop is executed where do I start counting[0,1,2,3,4,5,6], and when I reach the 6th element I cross it out(dequeue)right?

Is This A Good Question/Topic? 0
  • +

Replies To: How to count loops?

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,071
  • Joined: 10-May 07

Re: How to count loops?

Posted 04 March 2013 - 10:00 PM

When you create a new topic, there is text that clearly outlines not to give a title of 'help'. I renamed the title to be more descriptive of the question.
Was This Post Helpful? 0
  • +
  • -

#3 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: How to count loops?

Posted 04 March 2013 - 10:01 PM

Could you please tell us what kind of output you are expecting to get out of this?
Was This Post Helpful? 0
  • +
  • -

#4 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: How to count loops?

Posted 04 March 2013 - 10:05 PM

Output=Susan
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How to count loops?

Posted 04 March 2013 - 10:26 PM

your output is Susan, but you're printing everything that goes into your queue on line 21. Remove that and your program will work the way I think you're expecting it to.
Was This Post Helpful? 0
  • +
  • -

#6 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: How to count loops?

Posted 04 March 2013 - 10:32 PM

Sorry, this is a running program I got it from the textbook, Im just wondering how they got the final answer.

simqueue looks like this right:["Brad","Kent","Jane","Susan","David","Bill"]

Do they start count at Bill(0) and go to Brad(1), etc. and the cross off the 6th item and then start at zero again?
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How to count loops?

Posted 04 March 2013 - 10:44 PM

Look closely
while simqueue.size() > 1:
        for i in range(num):
            simqueue.enqueue(simqueue.dequeue())
        simqueue.dequeue()
return simqueue.dequeue()


Line 1 essentially says that we're gonna keep going until this queue only has 1 element left.

Line 2 says we're going to repeat the following x times (where x is the value you passed in as the 2nd argument)

Line 3 is where the magic happens. The last element is dequeued and then enqueued. That means that the last element gets moved to the front.

Line 4 says that after we've dequeued and enqueued x times, we dequeue one more time to get rid of the 'hot potato'

Line 5 will only activate when there is only 1 element left in the queue (to understand why, re-examine line 1). At this point, the only remaining element in the queue is dequeued and returned.

Not too hard when you break it down, yes?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1