# How to count loops?

Page 1 of 1

## 6 Replies - 1707 Views - Last Post: 04 March 2013 - 10:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314369&amp;s=9100dad3ade59f4e855a7f20bc9e0376&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 medaa

Reputation: 6
• 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()

```

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

• Professor Snuggly Pants

Reputation: 6223
• Posts: 29,819
• 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.

### #3 Nekroze

Reputation: 14
• 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?

### #4 medaa

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

## Re: How to count loops?

Posted 04 March 2013 - 10:05 PM

Output=Susan

### #5 atraub

• Pythoneer

Reputation: 827
• Posts: 2,231
• 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.

### #6 medaa

Reputation: 6
• 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.

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?

### #7 atraub

• Pythoneer

Reputation: 827
• Posts: 2,231
• 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?