MilenaPetkanova

FootballLeague80/100

Nov 3rd, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class Team
  7. {
  8. public string Name { get; set; }
  9. public int Points { get; set; }
  10. public int Goals { get; set; }
  11. }
  12.  
  13. class FootballLeague
  14. {
  15. static void Main()
  16. {
  17. var allTeams = new SortedDictionary<string, Team>();
  18.  
  19. string key = Console.ReadLine();
  20.  
  21. string play = Console.ReadLine();
  22.  
  23. while (play != "final")
  24. {
  25. Regex match = new Regex(@"(.*?)" + Regex.Escape(key)
  26. + @"(?<team1>[a-zA-Z]+)" + Regex.Escape(key)
  27. + @"(.*?)" + Regex.Escape(key)
  28. + @"(?<team2>[a-zA-Z]+)" + Regex.Escape(key)
  29. + @"(.*?)(?<result1>\d+):(?<result2>\d+)");
  30.  
  31. Match playMatch = match.Match(play);
  32.  
  33. string team1 = playMatch.Groups["team1"].Value;
  34. team1 = ReverseString(team1).ToUpper();
  35. string team2 = playMatch.Groups["team2"].Value;
  36. team2 = ReverseString(team2).ToUpper();
  37. int goalsT1 = int.Parse(playMatch.Groups["result1"].ToString());
  38. int goalsT2 = int.Parse(playMatch.Groups["result2"].ToString());
  39.  
  40.  
  41. if (!allTeams.ContainsKey(team1))
  42. {
  43. Team teamD = new Team
  44. {
  45. Name = team1,
  46. Points = 0,
  47. Goals = 0
  48. };
  49.  
  50. allTeams.Add(team1, teamD);
  51. }
  52. allTeams[team1].Points += CalculatePoints(goalsT1, goalsT2);
  53. allTeams[team1].Goals += goalsT1;
  54.  
  55. if (!allTeams.ContainsKey(team2))
  56. {
  57. Team teamG = new Team
  58. {
  59. Name = team2,
  60. Points = 0,
  61. Goals = 0
  62. };
  63.  
  64. allTeams.Add(team2, teamG);
  65. }
  66. allTeams[team2].Points += CalculatePoints(goalsT2, goalsT1);
  67. allTeams[team2].Goals += goalsT2;
  68.  
  69. play = Console.ReadLine();
  70. }
  71.  
  72. PrintResults(allTeams);
  73. }
  74.  
  75. private static void PrintResults(SortedDictionary<string, Team> allTeams)
  76. {
  77. var sortedByStandings = allTeams.Values.OrderByDescending(x => x.Points)
  78. .ToDictionary(x => x.Name, y => y.Points);
  79.  
  80. int standing = 1;
  81.  
  82. Console.WriteLine("League standings:");
  83. foreach (var team in sortedByStandings)
  84. {
  85. Console.WriteLine("{0}. {1} {2}", standing, team.Key, team.Value);
  86. standing++;
  87. }
  88.  
  89. var sortedByGoals = allTeams.Values.OrderByDescending(x => x.Goals)
  90. .ToDictionary(x => x.Name, y => y.Goals);
  91.  
  92. int top3 = 1;
  93.  
  94. Console.WriteLine("Top 3 scored goals:");
  95. foreach (var team in sortedByGoals)
  96. {
  97. if (top3 <= 3)
  98. {
  99. Console.WriteLine("- {0} -> {1}", team.Key, team.Value);
  100. }
  101. top3++;
  102. }
  103. }
  104.  
  105. public static string ReverseString(string s)
  106. {
  107. char[] charArray = s.ToCharArray();
  108. Array.Reverse(charArray);
  109. return new string(charArray);
  110. }
  111.  
  112. private static int CalculatePoints(int teamResult, int otherTeamResult)
  113. {
  114. int points = 0;
  115. if (teamResult == otherTeamResult)
  116. {
  117. points = 1;
  118. }
  119. else if (teamResult > otherTeamResult)
  120. {
  121. points = 3;
  122. }
  123. else if (teamResult < otherTeamResult)
  124. {
  125. points = 0;
  126. }
  127.  
  128. return points;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment