2. Race C# - Задача от Exercise: Regular Expressions
Здравейте, колеги,
задачата я решавам и Judge ми връща 100/100, но искам да попитам дали има някакъв "по-културен" начин за принтиране на топ 3 състезателите, без да използвам три пъти if и counter?
Ето го условието:
Write a program that processes information about a race.
On the first line you will be given list of participants separated by ", ".
On the next few lines until you receive a line "end of race" you will be given some info which will be some alphanumeric characters. In between them you could have some extra characters which you should ignore. For example: "G!32e%o7r#32g$235@!2e". The letters are the name of the person and the sum of the digits is the distance he ran. So here we have George who ran 29 km.
Store the information about the person only if the list of racers contains the name of the person. If you receive the same person more than once just add the distance to his old distance. At the end print the top 3 racers ordered by distance in descending in the format:
"1st place: {first racer}
2nd place: {second racer}
3rd place: {third racer}"
Input |
Output |
Comment |
George, Peter, Bill, Tom G4e@55or%6g6!68e!!@ R1@!3a$y4456@ B5@i@#123ll G@e54o$r6ge# 7P%et^#e5346r T$o553m&6 end of race |
1st place: George 2nd place: Peter 3rd place: Tom |
On the 3rd input line we have Ray. He is not in the list, so we do not count his result. The other ones are valid. George has total of 55 km, Peter has 25 and Tom has 19. We do not print Bill because he is on 4th place.
|
Ето го и моя код: https://pastebin.com/94dSEqEG
Благодаря!
Без каунт може да се направи във фор цикъл:
var result = dict.OrderByDescending(x => x.Value).Take(3).ToList();
for (int i = 1; i <= result.Count; i++) // или i <= 3. Take(3) става излишен
{
string place = i == 1 ? "st" : i == 2 ? "nd" : "rd";
Console.WriteLine($"{i}{place} place: {result[i - 1].Key}");
}