Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Queue:
- # Constructor creates a list
- def __init__(self):
- self.queue = list()
- # Adding elements to queue
- def enqueue(self, data):
- # Checking to avoid duplicate entry (not mandatory)
- if data not in self.queue:
- self.queue.insert(0, data)
- return True
- return False
- # Removing the last element from the queue
- def dequeue(self):
- if len(self.queue) > 0:
- return self.queue.pop()
- return 'Queue Empty!'
- # Getting the size of the queue
- def size(self):
- return len(self.queue)
- # printing the elements of the queue
- def Print(self):
- return self.queue
- def hotPotato(namelist, num):
- # initialise new queue with some data
- simqueue = Queue()
- for name in namelist:
- simqueue.enqueue(name)
- while simqueue.size() > 1:
- # after num count, dequeue and then enqueue immediately
- for i in range(num):
- simqueue.enqueue(simqueue.dequeue())
- # dequeue
- simqueue.dequeue()
- return simqueue.dequeue()
- print(hotPotato(["Bill","David","Susan","Jane","Kent","Brad"],17))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement