Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- class FootballLeague
- {
- static void Main(string[] args)
- {
- string key = Console.ReadLine();
- key = Regex.Escape(key);
- string inputLine = Console.ReadLine();
- string teamPattern = $@"(?:{key})(\w+)(?:{key})";
- string scorePattern = @"([0-9]*)(?:[:])([0-9]*)";
- List<Team> teams = new List<Team>();
- while (inputLine != "final")
- {
- MatchCollection matchedTeams = Regex.Matches(inputLine, teamPattern);
- MatchCollection matchedResult = Regex.Matches(inputLine, scorePattern);
- Team currentTeam = new Team();
- string leftTeamName = string.Join("", matchedTeams[0].Groups[1].ToString().Reverse()).ToUpper();
- long leftTeamGoals = long.Parse(matchedResult[0].Groups[1].ToString());
- string rightTeamName = string.Join("", matchedTeams[1].Groups[1].ToString().Reverse()).ToUpper();
- long rightTeamGoals = long.Parse(matchedResult[0].Groups[2].ToString());
- if (!teams.Any(t => t.Name == leftTeamName))
- {
- currentTeam = new Team();
- currentTeam.Name = leftTeamName;
- teams.Add(currentTeam);
- }
- if (!teams.Any(t => t.Name == rightTeamName))
- {
- currentTeam = new Team();
- currentTeam.Name = rightTeamName;
- teams.Add(currentTeam);
- }
- Team leftTeam = teams.First(t => t.Name == leftTeamName);
- Team rightTeam = teams.First(t => t.Name == rightTeamName);
- leftTeam.Goals += leftTeamGoals;
- rightTeam.Goals += rightTeamGoals;
- if (leftTeamGoals > rightTeamGoals)
- {
- leftTeam.Points += 3;
- }
- else if (leftTeamGoals < rightTeamGoals)
- {
- rightTeam.Points += 3;
- }
- else
- {
- leftTeam.Points += 1;
- rightTeam.Points += 1;
- }
- inputLine = Console.ReadLine();
- }
- PrintTeams(teams.OrderByDescending(t => t.Points).ThenBy(t=>t.Name).ToList());
- }
- static void PrintTeams(List<Team> teams)
- {
- Console.WriteLine("League standings:");
- for (int i = 0; i < teams.Count; i++)
- {
- Console.WriteLine($"{i + 1}. {teams[i].Name} {teams[i].Points}");
- }
- Console.WriteLine("Top 3 scored goals:");
- foreach (var team in teams.OrderByDescending(t => t.Goals).ThenBy(t=>t.Name).Take(3))
- {
- Console.WriteLine($"- {team.Name} -> {team.Goals}");
- }
- }
- }
- class Team
- {
- public string Name { get; set; }
- public int Points { get; set; }
- public long Goals { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement