LardaX

Football-League

Oct 20th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 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. namespace Football_League
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             char[] delimiters = new char[] { ' ', ':' };
  15.             string decryptPatter = Console.ReadLine();
  16.             string pattern = Regex.Escape(decryptPatter);
  17.             string input = Console.ReadLine();
  18.  
  19.             Dictionary<string, int> teamScores = new Dictionary<string, int>();
  20.             Dictionary<string, int> teamGoals = new Dictionary<string, int>();
  21.  
  22.             while (!input.Equals("final"))
  23.             {
  24.                 string[] teamInfo = Regex.Split(input, pattern);
  25.                 string[] scores = teamInfo[teamInfo.Length - 1].Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
  26.  
  27.                 string firstTeam = new string(teamInfo[1].ToUpper().Reverse().ToArray());
  28.                 string secondTeam = new string(teamInfo[teamInfo.Length - 2].ToUpper().Reverse().ToArray());
  29.  
  30.                 InsertTeam(teamScores, firstTeam);
  31.                 InsertTeam(teamGoals, firstTeam);
  32.                 InsertTeam(teamScores, secondTeam);
  33.                 InsertTeam(teamGoals, secondTeam);
  34.  
  35.                 int firstTeamScore = int.Parse(scores[scores.Length - 2]);
  36.                 int secondTeamScore = int.Parse(scores[scores.Length - 1]);
  37.  
  38.                 InsertGoals(teamGoals, firstTeamScore, firstTeam);
  39.                 InsertGoals(teamGoals, secondTeamScore, secondTeam);
  40.  
  41.                 InsertScores(teamScores, firstTeam, secondTeam, firstTeamScore, secondTeamScore);
  42.  
  43.                 input = Console.ReadLine();
  44.             }
  45.  
  46.             teamScores = teamScores.OrderByDescending(sc => sc.Value).ThenBy(name => name.Key).ToDictionary(x => x.Key, x => x.Value);
  47.  
  48.             int standing = 1;
  49.  
  50.             Console.WriteLine("League standings:");
  51.  
  52.             foreach (KeyValuePair<string, int> teamScore in teamScores)
  53.             {
  54.                 Console.WriteLine($"{standing}. {teamScore.Key.ToUpper()} {teamScore.Value}");
  55.                 standing++;
  56.             }
  57.  
  58.             teamGoals = teamGoals.OrderByDescending(goals => goals.Value).ThenBy(name => name.Key).Take(3).ToDictionary(x => x.Key, x => x.Value);
  59.  
  60.             Console.WriteLine("Top 3 scored goals:");
  61.             foreach (var pair in teamGoals)
  62.             {
  63.                 Console.WriteLine($"- {pair.Key.ToUpper()} -> {pair.Value}");
  64.             }
  65.         }
  66.  
  67.         private static void InsertGoals(Dictionary<string, int> teamGoals, int firstTeamScore, string firstTeam)
  68.         {
  69.             teamGoals[firstTeam] += firstTeamScore;
  70.         }
  71.  
  72.         private static void InsertScores(Dictionary<string, int> teamScores, string firstTeam, string secondTeam, int firstTeamScore, int secondTeamScore)
  73.         {
  74.             if (firstTeamScore > secondTeamScore)
  75.             {
  76.                 teamScores[firstTeam] += 3;
  77.             }
  78.             else if (secondTeamScore > firstTeamScore)
  79.             {
  80.                 teamScores[secondTeam] += 3;
  81.             }
  82.             else if (firstTeamScore == secondTeamScore)
  83.             {
  84.                 teamScores[firstTeam]++;
  85.                 teamScores[secondTeam]++;
  86.             }
  87.         }
  88.  
  89.         private static void InsertTeam(Dictionary<string, int> teamScores, string firstTeam)
  90.         {
  91.             if (!teamScores.ContainsKey(firstTeam))
  92.             {
  93.                 teamScores.Add(firstTeam, 0);
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment