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

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

    def Enqueue(self,item):
        return self.items.insert(0, item)

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

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

Q = Queue()    
def Hotpot(namelist,num):

    for name in namelist:
        Q.Enqueue(name)

    while Q.Size() > 1:
        for i in range(num):
           Q.Enqueue(Q.Dequeue())
        Q.Dequeue()
    return Q.Dequeue() # I would like to print and see what is getting removed, I tried with x = Q.Dequeue(), print x and print Q.Dequeue() but im getting "None"



print Hotpot(['A','B','C','D','E','F'],7)

Hi Team, I am trying above code for checking Queue, here I would like to print which value is removing each cycle. While printing I am getting none oly, please help me what changes I have to do for my expectation.

4
  • why don't you use collections.deque? Commented Aug 6, 2014 at 6:11
  • this code works for me. No None. Commented Aug 6, 2014 at 6:14
  • Hint: What do the loop above your return statement do? Commented Aug 6, 2014 at 6:14
  • Hi Daniel, Thanks for your reply, yes this code works for me as well it ll return C at last but I would like to print every item of removal one by one, print Q.Dequeue, when put this in program iam getting C and None Commented Aug 6, 2014 at 6:18

2 Answers 2

2

If you want to know what's goingto be returned, you need to save it locally, print what you saved, then return what you saved. Something like:

    x = Q.Dequeue()
    print(x)
    return x
Sign up to request clarification or add additional context in comments.

Comments

-1

Your code works for me, when you change Q.Dequeue() by print Q.Dequeue(). Better python would be:

from collections import deque

def hotspot(names, num):
    queue = deque(reversed(names))
    while len(queue)>1:
        queue.rotate(num)
        print queue.pop()
    return queue.pop()

print hotspot(['A','B','C','D','E','F'], 7)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.