+ 1

Drivers license challenge

I found a solution that works on my machine but not in the app and I can‘t figure out why... Here‘s my code: string name = Console.ReadLine(); int agents = Convert.ToInt32(Console.ReadLine()); string others = Console.ReadLine(); int endString1 = others.IndexOf(' ', 0); int endString2 = others.IndexOf(' ', endString1 + 1); int endString3 = others.IndexOf(' ', endString2 + 1); string name1 = others.Remove(endString1); string name2 = others.Substring(endString1 + 1, endString2 - 1); string name3 = others.Substring(endString2 + 1, endString3 -1); string name4 = others.Remove(0, endString3); string[] applicants= {name1, name2, name3, name4, name}; Array.Sort(applicants); double place = Array.IndexOf(applicants, name); double groups = Math.Ceiling(place/agents); int minutes = (int)groups *20; Console.WriteLine(minutes); Any ideas why?

7th Apr 2020, 3:49 PM
Jasmin Kaufmann
Jasmin Kaufmann - avatar
10 ответов
+ 1
https://code.sololearn.com/c91MM0DzLgDU/?ref=app you could simplify your code like this, you went through a lot just to split the names. double place =Array.IndexOf(applicants, name); you should add 1. because arrays star from zero but a queue of people starts from 1. unless the DMV is run by nerds.
7th Apr 2020, 4:33 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
oh wow! thanks! 😊 now i feel like an idiot 🤦‍♀️ (and to be honest when i started it my code had been even more complicated)
7th Apr 2020, 4:37 PM
Jasmin Kaufmann
Jasmin Kaufmann - avatar
+ 1
I am impressed by your approach of splitting by spaces. it's fine to try, but when someone already went through it to provide a function for it, use it lol. now you have to figure the number of minutes. when I first run your code there were some errors using substring(). but I couldn't bother to figure them out.
7th Apr 2020, 4:42 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
with your version the name variable is not in the array now though. so i guess i find another way go figure out which place in the queue it is
7th Apr 2020, 4:56 PM
Jasmin Kaufmann
Jasmin Kaufmann - avatar
+ 1
oh, right forgot to add it. just do : others += " "+name ; before the split function.
7th Apr 2020, 5:00 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
i created another new array, looped through the applicants, then added the name 🤦‍♀️😂
7th Apr 2020, 5:06 PM
Jasmin Kaufmann
Jasmin Kaufmann - avatar
+ 1
just add it to the string of others before split and don't forget to add 1 to place. double place = Array.IndexOf(applicants, name) +1 ; and you are good to go 👍
7th Apr 2020, 5:09 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Thanks a ton! I hope I’ll get to a point where I’m not overcomplicating everything some time in the distant future
7th Apr 2020, 5:11 PM
Jasmin Kaufmann
Jasmin Kaufmann - avatar
+ 1
you are welcome 😃 sure you will get there. the more you advance the lazy and creative you will be, you will always look for the easiest way to do things. using ready libraries, other people's code, stackoverflow... :)
7th Apr 2020, 5:16 PM
Bahhaⵣ
Bahhaⵣ - avatar
9th Apr 2020, 5:00 AM
Jonathan Alvarado
Jonathan Alvarado - avatar