JulianJulianov

05.RegularExpressionsExercise-Race

May 3rd, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. 02. Race
  2. 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:
  3. "1st place: {first racer}  
  4. 2nd place: {second racer}
  5. 3rd place: {third racer}"
  6. Examples
  7. Input                       Output             Comment
  8. George, Peter, Bill, Tom    1st place: George  On the 3rd input line we have Ray. He is not in the list, so we do not count his result.
  9. G4e@55or%6g6!68e!!@         2nd place: Peter   The other ones are valid. George has total of 55 km, Peter has 25 and Tom has 19.
  10. R1@!3a$y4456@               3rd place: Tom     We do not print Bill because he is on 4th place.
  11. B5@i@#123ll
  12. G@e54o$r6ge#
  13. 7P%et^#e5346r
  14. T$o553m&6
  15. end of race
  16.  
  17. using System;
  18. using System.Text.RegularExpressions;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21.  
  22. public class Program
  23. {
  24.     public static void Main()
  25.     {
  26.           var names = Console.ReadLine().Split(", ");
  27.         //var pattern = @"[A-Za-z]|[\d]";                       //Труден вариант
  28.           var listRacers = new Dictionary<string, int>();
  29.  
  30.           foreach (var item in names)
  31.           {
  32.               listRacers.Add(item, 0);
  33.           }
  34.           var command = string.Empty;
  35.           while ((command = Console.ReadLine()) != "end of race")
  36.           {
  37.               var name = "";
  38.               var sumKm = 0;
  39.             //var matchLettersAndDigits = Regex.Matches(command, pattern);
  40.               //foreach (Match item in matchLettersAndDigits)
  41.                 //{
  42.                     //try
  43.                     //{
  44.                         //if (Char.IsLetter((char)int.Parse(item.Value)) == false)
  45.                         //{
  46.                             //sumKm += int.Parse(item.Value);
  47.                         //}
  48.                     //}
  49.                     //catch (Exception)
  50.                     //{
  51.                         //name += item.Value;
  52.                     //}
  53.                 //}
  54.                
  55.                 //if (names.Contains(name))
  56.                 //{
  57.                     //if (!listRacers.ContainsKey(name))
  58.                     //{
  59.                         //listRacers.Add(name, sumKm);
  60.                     //}
  61.                     //else
  62.                     //{
  63.                         //listRacers[name] += sumKm;
  64.                     //}
  65.                 //}
  66.              
  67.               foreach (var item in command)                               //Лесен вариант
  68.               {
  69.                   if (Char.IsLetter(item))
  70.                   {
  71.                       name += item;
  72.                   }
  73.                   else if (Char.IsDigit(item))
  74.                   {
  75.                       var digit = int.Parse(item.ToString());
  76.                       sumKm += digit;
  77.                   }
  78.               }
  79.               if (listRacers.ContainsKey(name))
  80.               {
  81.                   listRacers[name] += sumKm;
  82.               }
  83.           }
  84.           var counter = 0;                                                      //С Take(3) взимам само първите три елемента!
  85.         //var topThreePlayers = listRacers.OrderByDescending(x => x.Value).Take(3).ToDictionary(x => x.Key, y => y.Value);
  86.           foreach (var item in listRacers.OrderByDescending(x => x.Value).Take(3)/*topThreePlayers*/)
  87.           {
  88.               counter++;
  89.               var twoLetters = "";
  90.               if (counter == 1)
  91.               {
  92.                   twoLetters = "st";
  93.               }
  94.               else if (counter == 2)
  95.               {
  96.                   twoLetters = "nd";
  97.               }
  98.               else
  99.               {
  100.                   twoLetters = "rd";
  101.               }
  102.               Console.WriteLine($"{counter}{twoLetters} place: {string.Join("", item.Key)}");
  103.               //if (counter == 1)
  104.               //{
  105.               //    Console.WriteLine($"1st place: {item.Key}");
  106.               //}
  107.               //else if (counter == 2)
  108.               //{
  109.               //    Console.WriteLine($"2nd place: {item.Key}");
  110.               //}
  111.               //else if (counter == 3)
  112.               //{
  113.               //    Console.WriteLine($"3rd place: {item.Key}");
  114.               //}
  115.           }
  116.     }
  117. }
Add Comment
Please, Sign In to add comment