I can't find any error in my code but I don't know why it's not working.Please help me fix it! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I can't find any error in my code but I don't know why it's not working.Please help me fix it!

It's a problem in the community section named "New Driver's License". Here's my code:-----------> https://code.sololearn.com/cr6S1B8I2K7y/?ref=app

22nd Jul 2023, 10:01 AM
Vaibhav Pandey
22 Answers
+ 4
Vaibhav Pandey what happens when there are 6 agents, or 7, or 8, or ... 32767 agents? Will you be able to add so much code? Take my advice. There is a better way.
22nd Jul 2023, 3:23 PM
Brian
Brian - avatar
+ 4
Vaibhav Pandey the code presented does not handle cases when there are 4 agents or more. In order to extend the code and solve the problem for an arbitrary number of agents, you will need to make a mathematical formula. The formula would account for your own 20-minute session and determine how many 20-minute sessions are ahead of you. That is determined by the number of people in line ahead of you and the number of agents that are processing them in parallel. Hint: use integer division. BTW, the solution does not require sorting. You can merely count how many in the line have a name that is less than or equal to yours.
22nd Jul 2023, 2:23 PM
Brian
Brian - avatar
+ 4
Congratulations for passing the challenge. But you should come back to improve your solution when you learn more practical techniques. You're missing a great learning oportunity here. How you solve it is more important. range with steps, list slicing and the 'in' check was what I used, and there was no need for hard coded quantities. All in 10 lines of code. Others here can probably do it in less...
22nd Jul 2023, 4:19 PM
Bob_Li
Bob_Li - avatar
+ 3
The names are called in alphabetical order. So maybe you should sort the list after appending your name?
22nd Jul 2023, 10:31 AM
Bob_Li
Bob_Li - avatar
+ 3
you shouldn't be hardcoding for all the cases....
22nd Jul 2023, 2:46 PM
Bob_Li
Bob_Li - avatar
+ 3
Bob_Li to turn your code into a four-line solution, you can replace the last five lines of your code with this: print(20*(1 + people.index(me)//agents))
24th Jul 2023, 12:05 PM
Brian
Brian - avatar
+ 2
Bob_Li I already did it bro by using the loop and appending the words in alphabetical order because I didn't know about sort() function.
22nd Jul 2023, 10:40 AM
Vaibhav Pandey
+ 2
Vaibhav Pandey Add this after last elif else: time = time + 20 And some conditions are useless: if agents == 1: #if only one agent is if agents == 1: #if only one agent is available. time = my_chance * 20 elif agents == 3: #if 3 agents are available. time = time + 40 else: time = time + 20
22nd Jul 2023, 11:57 AM
A͢J
A͢J - avatar
+ 2
name = input() agents = int(input()) people = input().split(" ") people.append(name) lst_people = people[:] new_people = [] for i in people: #Secuenciando los nombres. new_people.append(min(lst_people)) lst_people.remove(min(lst_people)) my_chance = new_people.index(name) + 1 time = 0 if agents == 1: # Si solo hay un agente disponible. time = my_chance * 20 elif agents == 2: # Si hay dos agentes disponibles. if my_chance <= 2: time = 20 elif my_chance <= 4: time = 40 else: time = 60 elif agents == 3: # Si hay tres agentes disponibles. if my_chance <= 3: time = 20 else: time = 40 elif agents == 4: # Si hay cuatro agentes disponibles. if my_chance == 5: time = 40 else: time = 20 else: time = 20 print(time) Use this code
24th Jul 2023, 3:12 AM
Emmanuel Gonzalez
Emmanuel Gonzalez - avatar
+ 2
Vaibhav Pandey Just put this code at place of your code. No need to check agent 2 or 4. Just try this: ---+++++--- name = input() agents = int(input()) people = input().split(" ") people.append(name) lst_people = people[:] new_people = [] for i in people: #Sequencing the name. new_people.append(min(lst_people)) lst_people.remove(min(lst_people)) my_chance = new_people.index(name) + 1 time = 0 if agents == 1: #if only one agent is if agents == 1: #if only one agent is available. time = my_chance * 20 elif agents == 3: #if 3 agents are available. time = 40 else: time = 20 print(time)
24th Jul 2023, 3:29 AM
A͢J
A͢J - avatar
+ 2
yes, it would complicate things if there are same names in the list. In the rare chance that everybody have the same name... They would have to fight it out in an epic battle to determine the order... https://globalnews.ca/news/7809395/josh-meme-fight-nebraska-name/ Sorting non-unique items like names is problematic. Unique id is more reliable.
24th Jul 2023, 12:54 PM
Bob_Li
Bob_Li - avatar
+ 1
@Brian Thank you bro for the advice it worked!!!
22nd Jul 2023, 3:57 PM
Vaibhav Pandey
+ 1
Brian I just added : else: time = time +20 in the last line and it worked!
22nd Jul 2023, 4:01 PM
Vaibhav Pandey
+ 1
Bob_Li Yes bro that's why I learn and do problems daily.
22nd Jul 2023, 4:27 PM
Vaibhav Pandey
+ 1
Congratulations Vaibhav Pandey! This approach to development is known as Test-Driven Development (TDD), wherein you write the code to pass the tests. Often the code is only as good as the tests. TDD is an acceptable approach that depends on having really good tests that push beyond the limits of the requirements. Unfortunately, Sololearn's tests are not so comprehensive and can lead students into feeling too confident about their code when it does not completely solve the problem. To Bob_Li's point about evolving as you learn, I have revised my solution to this problem a couple times as I learned more about Python. My first version was about 8 lines long. Now my code is 5 lines long - with the first 3 used for input. (It could be 4 lines, but I have an extra line to keep it readable). Keep thinking further about how to turn your code into a general solution for any valid input value. That is the hallmark of a lifelong learner.
22nd Jul 2023, 6:27 PM
Brian
Brian - avatar
+ 1
Brian Thanks bro I will keep this in my mind.
22nd Jul 2023, 6:33 PM
Vaibhav Pandey
+ 1
I'm curious about Brian 's 5 line solution. Here's mine: me = input() agents = int(input()) people = sorted((input().split()[:])+[me]) time = 20 for i in range(0,len(people),agents): if me in people[i:i+agents]: print(time) time+=20 I can't seem to compress the for loop part to a list comprehension or a generator expression... it should be possible, I think.
24th Jul 2023, 10:45 AM
Bob_Li
Bob_Li - avatar
+ 1
Brian 😎👍 you're right. I was so fixated on list slicing that I completely overlooked the index arithmetic approach... I could adapt this to my c, c++ and other language solutions as well. yes, 4 lines is nice.
24th Jul 2023, 12:11 PM
Bob_Li
Bob_Li - avatar
0
A͢J but bro the condition - if agents == 1: time = my_chance * 20 gives accurate value when only one agent is available by checking the name in the list .
22nd Jul 2023, 1:13 PM
Vaibhav Pandey
0
Brian Bro I made the code for 4 and 5 agents also but it's still not working.See the code bro.
22nd Jul 2023, 2:44 PM
Vaibhav Pandey