Advertisement
gospod1978

Regular Expression/Race

Oct 11th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Text.RegularExpressions;
  8.  
  9.  
  10. namespace Orders
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.  
  17.             string[] name = Console.ReadLine().Split(", ").ToArray();
  18.  
  19.             string namePattern = @"[A-Za-z]";
  20.             string racePattern = @"\d";
  21.  
  22.             string race = string.Empty;
  23.  
  24.             string currentName = string.Empty;
  25.  
  26.             List<string> newWin = new List<string>();
  27.  
  28.             var winner = new Dictionary<string, int>();
  29.  
  30.  
  31.             while ((race = Console.ReadLine()) != "end of race")
  32.             {
  33.  
  34.                 MatchCollection nameRegex = Regex.Matches(race, namePattern);
  35.  
  36.                 MatchCollection raceRegex = Regex.Matches(race, racePattern);
  37.  
  38.                 string newName = string.Empty;
  39.  
  40.                 foreach (Match match in nameRegex)
  41.                 {
  42.                     newName += match.Value;
  43.                 }
  44.                
  45.                 int kilom = 0;
  46.  
  47.                 for (int i = 0; i < name.Length; i++)
  48.                 {
  49.  
  50.                     currentName = name[i];
  51.                     if (currentName == newName)
  52.                     {
  53.                         foreach (Match racec in raceRegex)
  54.                         {
  55.                             kilom += int.Parse(racec.Value);
  56.                         }
  57.  
  58.                         if (!winner.ContainsKey(newName))
  59.                         {
  60.                             winner.Add(newName, kilom);
  61.                         }
  62.                         else
  63.                         {
  64.                             winner[newName] += kilom;
  65.                         }
  66.  
  67.                     }
  68.  
  69.                 }
  70.  
  71.             }
  72.  
  73.             var order = winner.OrderByDescending(x => x.Value);
  74.  
  75.             for (int j = 0; j < 3; j++)
  76.             {
  77.                 foreach (var kvp in order)
  78.                 {
  79.                     newWin.Add(kvp.Key);
  80.  
  81.                 }
  82.             }
  83.  
  84.             for (int k = 0; k < 3; k++)
  85.             {
  86.                 if (k == 0)
  87.                 {
  88.                     Console.WriteLine($"{k + 1}st place: {newWin[k]}");
  89.                 }
  90.                 else if (k == 1)
  91.                 {
  92.                     Console.WriteLine($"{k + 1}nd place: {newWin[k]}");
  93.                 }
  94.                 else
  95.                 {
  96.                     Console.WriteLine($"{k + 1}rd place: {newWin[k]}");
  97.                 }
  98.             }
  99.  
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement