Advertisement
Guest User

Untitled

a guest
Oct 29th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _03_Football_League
  7. {
  8. public class FootballLeague
  9. {
  10. public static void Main()
  11. {
  12. string keyWord = Regex.Escape(Console.ReadLine());
  13.  
  14. var teamRegex = new Regex($@"^.*(?:{keyWord})(?<team1>[A-Za-z]*)(?:{keyWord}).*(?:{keyWord})(?<team2>[A-Za-z]*)(?:{keyWord})");
  15. var scoreRegex = new Regex(@"(?<result>\d+\:\d+)");
  16.  
  17. string line = Console.ReadLine();
  18.  
  19. List<Team> teams = new List<Team>();
  20.  
  21. while (line != "final")
  22. {
  23. var teamMatches = teamRegex.Match(line);
  24.  
  25. string homeTeam = new string(teamMatches.Groups["team1"].Value.ToCharArray().Reverse().ToArray()).ToUpper();
  26. string awayTeam = new string(teamMatches.Groups["team2"].Value.ToCharArray().Reverse().ToArray()).ToUpper();
  27.  
  28. var score = scoreRegex.Match(line).Groups["result"].Value;
  29. var scoreTokens = score.Split(':').ToArray();
  30. var homeTeamGoals = long.Parse(scoreTokens[0]);
  31. var awayTeamGoals = long.Parse(scoreTokens[1]);
  32.  
  33. var homeTeamPoints = 0;
  34. var awayTeamPoints = 0;
  35. DistributePoints(homeTeamGoals, awayTeamGoals, ref homeTeamPoints, ref awayTeamPoints);
  36.  
  37. if (teams.Any(x => x.TeamName == homeTeam))
  38. {
  39. var existingHomeTeam = teams.First(x => x.TeamName == homeTeam);
  40. existingHomeTeam.Points += homeTeamPoints;
  41. existingHomeTeam.ScoredGoals += homeTeamGoals;
  42. }
  43. else
  44. {
  45. var team = new Team(homeTeam, homeTeamPoints, homeTeamGoals);
  46. teams.Add(team);
  47. }
  48.  
  49. if (teams.Any(x => x.TeamName == awayTeam))
  50. {
  51. var existingAwayTeam = teams.First(x => x.TeamName == awayTeam);
  52. existingAwayTeam.Points += awayTeamPoints;
  53. existingAwayTeam.ScoredGoals += awayTeamGoals;
  54. }
  55. else
  56. {
  57. var team = new Team(awayTeam, awayTeamPoints, awayTeamGoals);
  58. teams.Add(team);
  59. }
  60.  
  61. line = Console.ReadLine();
  62. }
  63. Console.WriteLine("League standings:");
  64. int cnt = 1;
  65. foreach (var team in teams.OrderByDescending(x => x.Points).ThenBy(x => x.TeamName))
  66. {
  67. Console.WriteLine($"{cnt}. {team.TeamName} {team.Points}");
  68. cnt++;
  69. }
  70. Console.WriteLine("Top 3 scored goals:");
  71. foreach (var team in teams.OrderByDescending(x => x.ScoredGoals).ThenBy(x => x.TeamName).Take(3))
  72. {
  73. Console.WriteLine($"- {team.TeamName} -> {team.ScoredGoals}");
  74. }
  75. }
  76.  
  77. private static void DistributePoints(long homeTeamGoals, long awayTeamGoals, ref int homeTeamPoints, ref int awayTeamPoints)
  78. {
  79. if (homeTeamGoals > awayTeamGoals)
  80. {
  81. homeTeamPoints = 3;
  82. }
  83. else if (awayTeamGoals > homeTeamGoals)
  84. {
  85. awayTeamPoints = 3;
  86. }
  87. else
  88. {
  89. homeTeamPoints = 1;
  90. awayTeamPoints = 1;
  91. }
  92. }
  93. }
  94.  
  95. public class Team
  96. {
  97. public Team(string teamName, int points, long scoredGoals)
  98. {
  99. TeamName = teamName;
  100. Points = points;
  101. ScoredGoals = scoredGoals;
  102. }
  103.  
  104. public string TeamName { get; set; }
  105.  
  106. public int Points { get; set; }
  107.  
  108. public long ScoredGoals { get; set; }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement