Advertisement
Ivelin_1936

Untitled

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