New Driving License

wordcloud

Note: This page contains a small but interesting piece of Python code which I call snippets. You can find more such codes on my Python snippets page.

Problem

You have to get a new driver's license and you show up at the office at the same time as 4 other people. The office says that they will see everyone in alphabetical order and it takes 20 minutes for them to process each new license. All of the agents are available now, and they can each see one customer at a time. How long will it take for you to walk out of the office with your new license?

Task
Given everyone's name that showed up at the same time, determine how long it will take to get your new license.

Input Format
Your input will be a string of your name, then an integer of the number of available agents, and lastly a string of the other four names separated by spaces.

Output Format
You will output an integer of the number of minutes that it will take to get your license.

Sample Input
'Eric'
2
'Adam Caroline Rebecca Frank'

Sample Output
40

Solution

Here is my solution to the above problem. Remember that there could be more than one way to solve a problem. If you have a more efficient or concise solution, please leave a comment.

In [1]:
name_self=input()
agents_count=int(input())
name_others=input()
names=name_others.split()
names.append(name_self)
names.sort()
print((int(names.index(name_self)/agents_count)+1)*20)
40

Explanation

My approach (or the algorithm)

  1. Find where you stand in the queue.
    • Add your name to the list of customers and sort it in alphabetical order.
    • Find your position in the sorted list.
  2. Customers are processed in batches equal to the number of agents. Each batch takes 20 minutes. Divide the number of people before you in the queue by the number of agents to get the number of batches that need to be processed before you go in. Make sure you round it down to an integer because if there is any decimal value, it only means that you are part of the next batch. For example, if there are 3 people before you and there are 2 agents to serve, then 3/2 = 1.5 (rounded down to 1) means that one batch of 2 people can be processed before you, and that you are part of the 2nd batch along with the 3rd person. Multiply that with 20 minutes.
  3. Add another 20 minutes to process your driving license.

The code

  • names=name_others.split() split names into a list.
  • names.append(name_self) add your name to it.
  • names.sort() sort in alphabetical order.
  • names.index(name_self) gives your position in the sorted list.
  • int(names.index(name_self)/agents_count) number of batches before you.
  • +1 add another batch to include your DL processing time before your multiply by 20.

The problem question is picked from SoloLearn. Here is my SoloLearn code and my SoloLearn profile page.

Last updated 2021-01-09 14:59:57.692308 IST

Comments