The challenge:

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.

My code:

def queue(queuers,pos):
    minutes = 0
    while 1:
        minutes += 1
        queuers[0] -= 1
        if queuers[0] > 0:
        elif queuers[pos] == 0:
        if pos > 0:
            pos -= 1
            pos = len(queuers)-1
        queuers = queuers[:]
    return minutes

print(queue([2, 5, 3, 6, 4], 0))

pythonic code*:

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?

  • 5
    \$\begingroup\$ If you want a review for your code, we can provide one, if this is some kind of initiation to a discussion forum then I suggest reddit is a better place. Note that for a code to be reviewed, it has to be a working code and be written by yourself. \$\endgroup\$ – bullseye Oct 8 '19 at 4:09
  • 1
    \$\begingroup\$ The code listed as "my code" is my code. It is working. The "pythonic code" below it is the top voted code for the same kata. I am asking for guidance on how I might get from where I am as a coder to the skill level demonstrated by the one liner. \$\endgroup\$ – jpolache Oct 8 '19 at 4:53
  • \$\begingroup\$ I fully understand. \$\endgroup\$ – bullseye Oct 8 '19 at 5:04
  • 2
    \$\begingroup\$ this might be interesting reading material for you: codereview.meta.stackexchange.com/questions/9353/… \$\endgroup\$ – dfhwze Oct 8 '19 at 12:59
  • 2
    \$\begingroup\$ Given that you both have and understand the cleaner code, and you ask 'how to get there', it seems like you want some broader advice on what books to read, etc. That kind of advice is unfortunately off-topic for CodeReview, so I agree with the other votes-to-close as too broad. \$\endgroup\$ – Reinderien Oct 9 '19 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.