Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2020
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5.  
  6. namespace Race
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. List<string> listOfNames = Console.ReadLine().Split(", ").ToList();
  13. Dictionary<string, int> bestPLayers = new Dictionary<string, int>();
  14. string input = Console.ReadLine();
  15.  
  16. string patternNames = @"[A-Za-z]";
  17. string patternNums = @"\d";
  18.  
  19. while (input != "end of race")
  20. {
  21. Regex regexNames = new Regex(patternNames);
  22. Regex regexNums = new Regex(patternNums);
  23.  
  24. var matchedNames = regexNames.Matches(input);
  25. var matchedNums = regexNums.Matches(input);
  26.  
  27. string name = String.Empty;
  28. int kilometers = 0;
  29.  
  30. name = BuildTheName(matchedNames, name);
  31. kilometers = CalculateKilometers(matchedNums, kilometers);
  32.  
  33. IsPlayerValid(listOfNames, bestPLayers, name, kilometers);
  34.  
  35. input = Console.ReadLine();
  36. }
  37.  
  38. var topThreePlayers = bestPLayers.OrderByDescending(x => x.Value).Take(3).ToDictionary(x => x.Key, y => y.Value);
  39.  
  40. int counter = 0;
  41.  
  42. foreach (var player in topThreePlayers)
  43. {
  44. counter++;
  45. if (counter == 1)
  46. {
  47. Console.WriteLine($"{counter}st place: {player.Key}");
  48. }
  49. else if (counter == 2)
  50. {
  51. Console.WriteLine($"{counter}nd place: {player.Key}");
  52. }
  53. else
  54. {
  55. Console.WriteLine($"{counter}rd place: {player.Key}");
  56. }
  57.  
  58. }
  59. }
  60.  
  61. private static void IsPlayerValid(List<string> listOfNames, Dictionary<string, int> bestPLayers, string name, int kilometers)
  62. {
  63. if (listOfNames.Contains(name))
  64. {
  65. if (!bestPLayers.ContainsKey(name))
  66. {
  67. bestPLayers[name] = 0;
  68. }
  69. bestPLayers[name] += kilometers;
  70.  
  71. }
  72. }
  73.  
  74. private static int CalculateKilometers(MatchCollection matchedNums, int kilometers)
  75. {
  76. foreach (Match digit in matchedNums)
  77. {
  78. kilometers += int.Parse(digit.Value);
  79. }
  80.  
  81. return kilometers;
  82. }
  83.  
  84. private static string BuildTheName(MatchCollection matchedNames, string name)
  85. {
  86. foreach (Match letter in matchedNames)
  87. {
  88. name += letter.Value;
  89. }
  90.  
  91. return name;
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement