You managed to send your friend to queue for tickets in your stead, but there is a catch: he will get there only if you tell him how much that is going to take. And everybody can only take one ticket at a time, then they go back in the last position of the queue if they need more (or go home if they are fine).
Each ticket takes one minutes to emit, the queue is well disciplined, Brit-style, and so it moves smoothly, with no waste of time.
You will be given an array/list/vector with all the people queuing and the initial position of your buddy, so for example, knowing that your friend is in the third position (that we will consider equal to the index, 2: he is the guy that wants 3 tickets!) and the initial queue is [2, 5, 3, 4, 6].
The first dude gets his ticket and the queue goes now like this [5, 3, 4, 6, 1], then [3, 4, 6, 1, 4] and so on. In the end, our buddy will be queuing for 12 minutes, true story!
Build a function to compute it, resting assured that only positive integers are going to be there and you will be always given a valid index; but we also want to go to pretty popular events, so be ready for big queues with people getting plenty of tickets.
def queue(queuers,pos): minutes = 0 while 1: minutes += 1 queuers -= 1 if queuers > 0: queuers.append(queuers) elif queuers[pos] == 0: break queuers.pop(0) if pos > 0: pos -= 1 else: pos = len(queuers)-1 queuers = queuers[:] return minutes print(queue([2, 5, 3, 6, 4], 0))
def queue(queuers,pos): return sum(min(queuer, queuers[pos] - (place > pos)) for place, queuer in enumerate(queuers)) print(queue([2, 5, 3, 6, 4], 0))
*top voted code for the same kata, I am not the author of this
I obviously am not at that level. How can I get there? I understand (more or less) the pythonic one liners when I see them. I'm just not yet at the point where I think that way from the get go. Any suggestions?