Advertisement
StoyanGrigorov

Untitled

Oct 20th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7. //03. Football League
  8. namespace FootballStandings
  9. {
  10.     class FootballStandings
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string key = Regex.Escape(Console.ReadLine());
  15.  
  16.             Dictionary<string, long> teamScores = new Dictionary<string, long>();
  17.             Dictionary<string, long> teamGoals = new Dictionary<string, long>();
  18.  
  19.             string pattern = string.Format(key + @"([a-zA-Z]*)" + key + @".*" + key + @"([a-zA-Z]*)" + key + @".*[^\d]" + @"(\d+):(\d+)");
  20.             Regex rgx1 = new Regex(pattern);
  21.  
  22.             string infoLine = Console.ReadLine();
  23.  
  24.             while (infoLine != "final")
  25.             {
  26.                 Match matchInfo = rgx1.Match(infoLine);
  27.  
  28.                 string teamOne = GetTeamName(matchInfo.Groups[1].Value.ToUpper());
  29.                 string teamTwo = GetTeamName(matchInfo.Groups[2].Value.ToUpper());
  30.                 long scoreTeamOne = long.Parse(matchInfo.Groups[3].Value);
  31.                 long scoreTeamTwo = long.Parse(matchInfo.Groups[4].Value);
  32.  
  33.                 if (teamGoals.ContainsKey(teamOne))
  34.                 {
  35.                     teamGoals[teamOne] += scoreTeamOne;
  36.                 }
  37.                 else
  38.                 {
  39.                     teamGoals.Add(teamOne, scoreTeamOne);
  40.                 }
  41.                 if (teamGoals.ContainsKey(teamTwo))
  42.                 {
  43.                     teamGoals[teamTwo] += scoreTeamTwo;
  44.                 }
  45.                 else
  46.                 {
  47.                     teamGoals.Add(teamTwo, scoreTeamTwo);
  48.                 }
  49.  
  50.                 if (scoreTeamOne > scoreTeamTwo)
  51.                 {
  52.                     if (teamScores.ContainsKey(teamOne))
  53.                     {
  54.                         teamScores[teamOne] += 3;
  55.                     }
  56.                     else
  57.                     {
  58.                         teamScores.Add(teamOne, 3);
  59.                     }
  60.                     if (teamScores.ContainsKey(teamTwo))
  61.                     {
  62.                         teamScores[teamTwo] += 0;
  63.                     }
  64.                     else
  65.                     {
  66.                         teamScores.Add(teamTwo, 0);
  67.                     }
  68.                 }
  69.                 else if (scoreTeamOne < scoreTeamTwo)
  70.                 {
  71.                     if (teamScores.ContainsKey(teamOne))
  72.                     {
  73.                         teamScores[teamOne] += 0;
  74.                     }
  75.                     else
  76.                     {
  77.                         teamScores.Add(teamOne, 0);
  78.                     }
  79.                     if (teamScores.ContainsKey(teamTwo))
  80.                     {
  81.                         teamScores[teamTwo] += 3;
  82.                     }
  83.                     else
  84.                     {
  85.                         teamScores.Add(teamTwo, 3);
  86.                     }
  87.                 }
  88.                 else
  89.                 {
  90.                     if (teamScores.ContainsKey(teamOne))
  91.                     {
  92.                         teamScores[teamOne] += 1;
  93.                     }
  94.                     else
  95.                     {
  96.                         teamScores.Add(teamOne, 1);
  97.                     }
  98.                     if (teamScores.ContainsKey(teamTwo))
  99.                     {
  100.                         teamScores[teamTwo] += 1;
  101.                     }
  102.                     else
  103.                     {
  104.                         teamScores.Add(teamTwo, 1);
  105.                     }
  106.                 }
  107.  
  108.                 infoLine = Console.ReadLine();
  109.             }
  110.             int place = 1;
  111.             Console.WriteLine("League standings:");
  112.             foreach (var team in teamScores.OrderByDescending(x=> x.Value).ThenBy(z => z.Key))
  113.             {
  114.                 Console.WriteLine($"{place}. {team.Key} {team.Value}");
  115.                 place++;
  116.             }
  117.  
  118.             Console.WriteLine("Top 3 scored goals:");
  119.             foreach (var topTeam in teamGoals.OrderByDescending(x=> x.Value).ThenBy(z=> z.Key).Take(3))
  120.             {
  121.                 Console.WriteLine($"- {topTeam.Key} -> {topTeam.Value}");
  122.             }
  123.         }
  124.  
  125.         private static string GetTeamName(string value)
  126.         {
  127.             string name = string.Join("", value.Reverse());
  128.             return name;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement