Advertisement
yanass

Race _ Regex _ Solved

Jul 31st, 2019
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Race
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             string[] names = Console.ReadLine()
  13.                 .Split(", ")
  14.                 .ToArray();
  15.  
  16.             Dictionary<string, int> racers = new Dictionary<string, int>();
  17.  
  18.             for (int i = 0; i < names.Length; i++)
  19.             {
  20.                 racers.Add(names[i], 0);
  21.             }
  22.  
  23.             string racerInfo = Console.ReadLine();
  24.  
  25.             while (racerInfo != "end of race")
  26.             {
  27.                 string pattern = @"(?<name>[A-Za-z]+)|(?<distance>\d)";
  28.  
  29.  
  30.                 MatchCollection nameDistance = Regex.Matches(racerInfo, pattern);
  31.  
  32.                 string name = "";
  33.  
  34.                 int splitDigits = 0;
  35.  
  36.                 foreach (Match item in nameDistance)
  37.                 {
  38.                     foreach (var letter in item.Groups["name"].Value)
  39.                     {
  40.                         name += letter;
  41.                     }
  42.  
  43.                     foreach (var digit in item.Groups["distance"].Value)
  44.                     {
  45.                         splitDigits += int.Parse(item.Groups["distance"].Value);
  46.                     }
  47.                 }
  48.  
  49.                 if (racers.ContainsKey(name))
  50.                 {
  51.                     racers[name] += splitDigits;
  52.                 }
  53.                 racerInfo = Console.ReadLine();
  54.             }
  55.  
  56.             int counter = 1;
  57.             foreach (var racer in racers.OrderByDescending(x=>x.Value).Take(3))
  58.             {
  59.                 string place = counter == 1 ? "st" : counter == 2 ? "nd" : "rd";
  60.  
  61.                 Console.WriteLine($"{counter}{place} place: {racer.Key}");
  62.  
  63.                 counter++;
  64.             }
  65.  
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement