Advertisement
YavorGrancharov

03.Football_League

Nov 5th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 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.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string key = Console.ReadLine();
  13.  
  14.             key = Regex.Escape(key);
  15.  
  16.             string pattern = $@"{key}(?<teamA>.*?){key}.+?{key}(?<teamB>.*?){key}.+?(?<goalsTeamA>\d+):(?<goalsTeamB>\d+)";
  17.  
  18.             string input = Console.ReadLine();
  19.  
  20.             Dictionary<string, int> standings = new Dictionary<string, int>();
  21.             Dictionary<string, int> goals = new Dictionary<string, int>();
  22.  
  23.             while (input != "final")
  24.             {
  25.                 MatchCollection teamsScores = Regex.Matches(input, pattern);
  26.  
  27.                 foreach (Match team in teamsScores)
  28.                 {
  29.                     string teamA = new string(team.Groups["teamA"].Value.Reverse().ToArray()).ToUpper();
  30.                     string teamB = new string(team.Groups["teamB"].Value.Reverse().ToArray()).ToUpper();
  31.                     int goalsTeamA = int.Parse(team.Groups["goalsTeamA"].Value);
  32.                     int goalsTeamB = int.Parse(team.Groups["goalsTeamB"].Value);
  33.  
  34.                     int scoresTeamA = 0;
  35.                     int scoresTeamB = 0;
  36.  
  37.                     if (goalsTeamA > goalsTeamB)
  38.                     {
  39.                         scoresTeamA = 3;
  40.                         scoresTeamB = 0;
  41.                     }
  42.                     else if (goalsTeamA < goalsTeamB)
  43.                     {
  44.                         scoresTeamB = 3;
  45.                         scoresTeamA = 0;
  46.                     }
  47.                     else
  48.                     {
  49.                         scoresTeamA = 1;
  50.                         scoresTeamB = 1;
  51.                     }
  52.  
  53.                     if (!standings.ContainsKey(teamA))
  54.                     {
  55.                         standings.Add(teamA, 0);
  56.                     }                  
  57.                     if (!standings.ContainsKey(teamB))
  58.                     {
  59.                         standings.Add(teamB, 0);
  60.                     }
  61.  
  62.                     standings[teamA] += scoresTeamA;
  63.                     standings[teamB] += scoresTeamB;
  64.  
  65.                     if (!goals.ContainsKey(teamA))
  66.                     {
  67.                         goals.Add(teamA, 0);
  68.                     }                    
  69.                     if (!goals.ContainsKey(teamB))
  70.                     {
  71.                         goals.Add(teamB, 0);
  72.                     }
  73.  
  74.                     goals[teamA] += goalsTeamA;
  75.                     goals[teamB] += goalsTeamB;
  76.                 }
  77.                 input = Console.ReadLine();
  78.             }
  79.             int indexer = 1;
  80.             Console.WriteLine("League standings:");
  81.             foreach (var team in standings.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  82.             {                
  83.                 Console.WriteLine("{0}. {1} {2}",indexer,team.Key,team.Value);
  84.                 indexer++;
  85.             }
  86.             Console.WriteLine("Top 3 scored goals:");
  87.             foreach (var team in goals.OrderByDescending(x => x.Value).ThenBy(x => x.Key).Take(3))
  88.             {
  89.                 Console.WriteLine("- {0} -> {1}",team.Key,team.Value);
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement