Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. class FootballLeague
  9. {
  10.     static void Main(string[] args)
  11.     {
  12.         string key = Console.ReadLine();
  13.         key = Regex.Escape(key);
  14.         string inputLine = Console.ReadLine();
  15.         string teamPattern = $@"(?:{key})(\w+)(?:{key})";
  16.         string scorePattern = @"([0-9]*)(?:[:])([0-9]*)";
  17.  
  18.         List<Team> teams = new List<Team>();
  19.  
  20.         while (inputLine != "final")
  21.         {
  22.             MatchCollection matchedTeams = Regex.Matches(inputLine, teamPattern);
  23.             MatchCollection matchedResult = Regex.Matches(inputLine, scorePattern);
  24.             Team currentTeam = new Team();
  25.             string leftTeamName = string.Join("", matchedTeams[0].Groups[1].ToString().Reverse()).ToUpper();
  26.             long leftTeamGoals = long.Parse(matchedResult[0].Groups[1].ToString());
  27.  
  28.             string rightTeamName = string.Join("", matchedTeams[1].Groups[1].ToString().Reverse()).ToUpper();
  29.             long rightTeamGoals = long.Parse(matchedResult[0].Groups[2].ToString());
  30.  
  31.             if (!teams.Any(t => t.Name == leftTeamName))
  32.             {
  33.                 currentTeam = new Team();
  34.                 currentTeam.Name = leftTeamName;
  35.                 teams.Add(currentTeam);
  36.             }
  37.  
  38.             if (!teams.Any(t => t.Name == rightTeamName))
  39.             {
  40.                 currentTeam = new Team();
  41.                 currentTeam.Name = rightTeamName;
  42.                 teams.Add(currentTeam);
  43.             }
  44.  
  45.             Team leftTeam = teams.First(t => t.Name == leftTeamName);
  46.             Team rightTeam = teams.First(t => t.Name == rightTeamName);
  47.             leftTeam.Goals += leftTeamGoals;
  48.             rightTeam.Goals += rightTeamGoals;
  49.             if (leftTeamGoals > rightTeamGoals)
  50.             {
  51.                 leftTeam.Points += 3;
  52.             }
  53.             else if (leftTeamGoals < rightTeamGoals)
  54.             {
  55.                 rightTeam.Points += 3;
  56.             }
  57.             else
  58.             {
  59.                 leftTeam.Points += 1;
  60.                 rightTeam.Points += 1;
  61.             }
  62.  
  63.             inputLine = Console.ReadLine();
  64.         }
  65.  
  66.         PrintTeams(teams.OrderByDescending(t => t.Points).ThenBy(t=>t.Name).ToList());
  67.     }
  68.  
  69.     static void PrintTeams(List<Team> teams)
  70.     {
  71.         Console.WriteLine("League standings:");
  72.         for (int i = 0; i < teams.Count; i++)
  73.         {
  74.             Console.WriteLine($"{i + 1}. {teams[i].Name} {teams[i].Points}");
  75.         }
  76.  
  77.         Console.WriteLine("Top 3 scored goals:");
  78.         foreach (var team in teams.OrderByDescending(t => t.Goals).ThenBy(t=>t.Name).Take(3))
  79.         {
  80.             Console.WriteLine($"- {team.Name} -> {team.Goals}");
  81.         }
  82.     }
  83. }
  84.  
  85. class Team
  86. {
  87.     public string Name { get; set; }
  88.     public int Points { get; set; }
  89.     public long Goals { get; set; }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement