Advertisement
simonradev

FootballLeague

Feb 16th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 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.  
  7. namespace FootballLeague
  8. {
  9.     public class FootballLeague
  10.     {
  11.         public static void Main()
  12.         {
  13.             string decryptionKey = Console.ReadLine();
  14.  
  15.             string pattern = Regex.Escape(decryptionKey) + "(.*?)" + Regex.Escape(decryptionKey);
  16.             Regex footballTeam = new Regex(pattern);
  17.  
  18.             Dictionary<string, long> leagueStandings = new Dictionary<string, long>();
  19.             Dictionary<string, long> topScoredGoalds = new Dictionary<string, long>();
  20.  
  21.             string inputLine = Console.ReadLine();
  22.             while (inputLine != "final")
  23.             {
  24.                 string[] matchInfo = inputLine.Split();
  25.  
  26.                 string firstTeamName = ReverseTeamName(footballTeam
  27.                                                     .Match(matchInfo[0])
  28.                                                     .Groups[1]
  29.                                                     .ToString()
  30.                                                     .ToUpper());
  31.                 string secondTeamName = ReverseTeamName(footballTeam
  32.                                                     .Match(matchInfo[1])
  33.                                                     .Groups[1]
  34.                                                     .ToString()
  35.                                                     .ToUpper());
  36.  
  37.                 string[] scoredGoalsInfo = matchInfo[matchInfo.Length - 1].Split(':');
  38.  
  39.                 long firstTeamGoals = long.Parse(scoredGoalsInfo[0]);
  40.                 long secondTeamGoals = long.Parse(scoredGoalsInfo[1]);
  41.  
  42.                 int firstTeamPoints = 0;
  43.                 int secondTeamPoints = 0;
  44.                 if (firstTeamGoals == secondTeamGoals)
  45.                 {
  46.                     firstTeamPoints = 1;
  47.                     secondTeamPoints = 1;
  48.                 }
  49.                 else
  50.                 {
  51.                     firstTeamPoints = firstTeamGoals > secondTeamGoals ? 3 : 0;
  52.                     secondTeamPoints = firstTeamGoals < secondTeamGoals ? 3 : 0;
  53.                 }
  54.  
  55.                 //filling the teams points and goals
  56.                 if (!leagueStandings.ContainsKey(firstTeamName) &&
  57.                     !topScoredGoalds.ContainsKey(firstTeamName))
  58.                 {
  59.                     leagueStandings.Add(firstTeamName, 0);
  60.                     topScoredGoalds.Add(firstTeamName, 0);
  61.                 }
  62.  
  63.                 leagueStandings[firstTeamName] += firstTeamPoints;
  64.                 topScoredGoalds[firstTeamName] += firstTeamGoals;
  65.  
  66.                 if (!leagueStandings.ContainsKey(secondTeamName) &&
  67.                     !topScoredGoalds.ContainsKey(secondTeamName))
  68.                 {
  69.                     leagueStandings.Add(secondTeamName, 0);
  70.                     topScoredGoalds.Add(secondTeamName, 0);
  71.                 }
  72.  
  73.                 leagueStandings[secondTeamName] += secondTeamPoints;
  74.                 topScoredGoalds[secondTeamName] += secondTeamGoals;
  75.  
  76.                 inputLine = Console.ReadLine();
  77.             }
  78.  
  79.             Console.WriteLine("League standings:");
  80.             int placeInStandings = 1;
  81.             foreach (KeyValuePair<string, long> pair in leagueStandings
  82.                                                         .OrderByDescending(p => p.Value)
  83.                                                         .ThenBy(n => n.Key))
  84.             {
  85.                 Console.WriteLine($"{placeInStandings}. {pair.Key} {pair.Value}");
  86.  
  87.                 placeInStandings++;
  88.             }
  89.  
  90.             Dictionary<string, long> topThreeScoredGoals = topScoredGoalds
  91.                                                             .OrderByDescending(g => g.Value)
  92.                                                             .ThenBy(n => n.Key)
  93.                                                             .Take(3)
  94.                                                             .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
  95.             Console.WriteLine("Top 3 scored goals:");
  96.             foreach (KeyValuePair<string, long> pair in topThreeScoredGoals)
  97.             {
  98.                 Console.WriteLine($"- {pair.Key} -> {pair.Value}");
  99.             }
  100.         }
  101.  
  102.         public static string ReverseTeamName(string teamName)
  103.         {
  104.             StringBuilder toReturn = new StringBuilder();
  105.             for (int currLetter = 0; currLetter < teamName.Length; currLetter++)
  106.             {
  107.                 toReturn.Append(teamName[teamName.Length - 1 - currLetter]);
  108.             }
  109.  
  110.             return toReturn.ToString();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement