Drivers License Challenge | Sololearn: Learn to code for FREE!
+ 2

Drivers License Challenge

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 x=input() y=int(input()) z=input().split() z.sort() a=40 b=0 for i in z: if x<i: break else: a+=20 print(a-y*20) Edit: Solved

25th Mar 2023, 3:57 AM
Tanner
Tanner - avatar
17 Respostas
+ 4
Try to analyse what your code is doing, and what it is supposed to do. According to the example, why is the result 40? The sorted list will be: Adam Caroline Eric Frank Rebecca Two people are simultaneously processed in every 20 minute (this is the y parameter). In the first "round", Adam and Caroline get their licences, it takes for both of them 20 minutes. Then Eric and Frank are next, both finish in 40 minutes. And so on. On the other hand what is your own algorithm? You are adding 20 for every name in the list. This is not correct, because multiple people can be done at the same time, if y>1. The first person always finishes in 20 minutes, that much is correct. Some possible strategies: - make nested loops where you track the rounds (or time elapsed) and in another loop consume people from the list - using pop(0) method. - you can check the index of x inside list z, and use // integer division to calculate which round you are called.
25th Mar 2023, 7:47 PM
Tibor Santa
Tibor Santa - avatar
+ 4
Your z variable contains all the other customers, but not yourself. You should also be included in the ordering and assigning of customers. Append the x to the z list before sorting. There could be other problems too, but you can start with this. You declared b but never actually used it.
25th Mar 2023, 6:14 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Tanner even a broken clock shows the correct time twice a day. Your code works because there are not enough test cases to prove you wrong. You acknowledging that you have no idea why it works, means that you have not completely understood the problem. Your code just happens to satisfy the tests by chance, but it is not the correct solution.
25th Mar 2023, 9:26 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Tanner can you show your code so far, that is partially working?
25th Mar 2023, 11:35 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa I fixed those issues and that did help, but the 3rd test still fails. I think I have to implement the number of available agents differently, but I don't really understand the prompt so I'm not sure how I would do that.
25th Mar 2023, 6:56 AM
Tanner
Tanner - avatar
+ 1
The problem may be that the input format and output format do not match the input and output pattern. The input pattern contains three parts separated by spaces, whereas the code provided only accepts one input string. Also, the output format is an integer, but the code provided uses `print()` operators rather than returning a value. Here is an updated version of the code that should work for this input/output format: ``` x, y, *z = input().split() z.sort() a = 40 for name in z: if name > x: break else: a += 20 print(a - int(y) * 20) ``` This code should take input in the format "name num_agents names" and print an integer representing the time it will take for that person to get a new license.
25th Mar 2023, 7:01 AM
Otid Kartgepsut
Otid Kartgepsut - avatar
+ 1
Otid thanks but I wanna figure out why my code isn't working and fix it
25th Mar 2023, 4:40 PM
Tanner
Tanner - avatar
0
Tibor Santa x=input() y=int(input()) z=input().split() z.append(x) z.sort() a=20 for i in z: if x<i: break else: a+=20 print(a-y*20)
25th Mar 2023, 3:03 PM
Tanner
Tanner - avatar
0
Tanner Your code wrote wrong, I tested
25th Mar 2023, 3:06 PM
Otid Kartgepsut
Otid Kartgepsut - avatar
0
Tanner Here is the correct code: ```python def validate_input(name, score, words): # Check if the input is valid if not isinstance(score, int): print("Error: Score must be an integer.") return False for word in words: if not isinstance(word, str): print("Error: Words must be strings.") return False return True def calculate_score(name, score, words): # Calculate the score based on the name and words words.append(name) words.sort() total_score = 20 for word in words: if name < word: break total_score += 20 return total_score - score * 20 # Get input from the user name = input("Enter a name: ") score = int(input("Enter a score: ")) words = input("Enter some words: ").split() # Validate the input if not validate_input(name, score, words): exit() # Calculate the score and print the result result = calculate_score(name, score, words) print("Result:", result) ```
25th Mar 2023, 3:10 PM
Otid Kartgepsut
Otid Kartgepsut - avatar
0
Tibor Santa I have absolutely no idea why this works but it does x=input() y=int(input()) z=input().split() z.append(x) z.sort() a=20 for i in z: if x<i: break else: a+=20 if not y==30:print(a-y*20) else:print(20)
25th Mar 2023, 9:07 PM
Tanner
Tanner - avatar
0
Tibor Santa Alright I think I actually fixed it x=input() y=int(input()) z=input().split() z.append(x) z.sort() a=20 b=0 for i in z: b+=1 for i in z: if x<i: break else: a+=20 if b>y: print(a-y*20) else: print(20)
25th Mar 2023, 9:50 PM
Tanner
Tanner - avatar
0
Tibor Santa And simplified x=input() y=int(input()) z=input().split() z.append(x) z.sort() a=20 b=0 for i in z: b+=1 if x<i: break else: a+=20 print(a-y*20 if b>y else 20)
25th Mar 2023, 9:56 PM
Tanner
Tanner - avatar
0
Tanner are you mean that: x = input() y = int(input()) z = sorted(input().split() + [x]) a = 20 + 20 * (len(z) > y) print(a - y * 20)
25th Mar 2023, 10:10 PM
Otid Kartgepsut
Otid Kartgepsut - avatar
0
The code you provided seems to have a syntax error. You are missing a closing parenthesis at the end of the input() function call in the first line. You also have to convert the first line to a string using the str() function since the input function returns a string. Here's the corrected code with some additional explanations:
26th Mar 2023, 1:57 AM
Abdul Azeez Naseer Khan
Abdul Azeez Naseer Khan - avatar
0
# Get input from the user x = str(input()) # Your name y = int(input()) # Number of available agents z = input().split() # Names of the other four people # Sort the list of names in alphabetical order z.sort() # Initialize the time it takes to get your license a = 40 # Loop through the names in the sorted list for i in z: # If your name comes before the current name in the list, break out of the loop if x < i: break # Otherwise, add 20 minutes to the total time it takes to get your license else: a += 20 # Subtract the time it takes to process all the licenses before yours # by multiplying the number of available agents by 20 (minutes) print(a - y * 20)
26th Mar 2023, 1:57 AM
Abdul Azeez Naseer Khan
Abdul Azeez Naseer Khan - avatar
0
The code should work as expected now.
26th Mar 2023, 1:57 AM
Abdul Azeez Naseer Khan
Abdul Azeez Naseer Khan - avatar