Advertisement
YavorGrancharov

Football_League(helped)

Sep 1st, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace 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}(?<teamOne>.*?){key}.+?{key}(?<teamTwo>.*?){key}.+?(?<teamOneGoals>\d+):(?<teamTwoGoals>\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 teams = Regex.Matches(input, pattern);
  26.  
  27.                 foreach (Match team in teams)
  28.                 {
  29.                     string teamOne = new string(team.Groups["teamOne"].Value.Reverse().ToArray()).ToUpper();
  30.                     string teamTwo = new string(team.Groups["teamTwo"].Value.Reverse().ToArray()).ToUpper();
  31.                     var teamOneGoals = int.Parse(team.Groups["teamOneGoals"].Value);
  32.                     var teamTwoGoals = int.Parse(team.Groups["teamTwoGoals"].Value);
  33.  
  34.                     var teamOnePoints = 0;
  35.                     var teamTwoPoints = 0;
  36.  
  37.                     if (teamOneGoals > teamTwoGoals)
  38.                     {
  39.                         teamOnePoints = 3;
  40.                         teamTwoPoints = 0;
  41.                     }
  42.                     else if (teamOneGoals < teamTwoGoals)
  43.                     {
  44.                         teamOnePoints = 0;
  45.                         teamTwoPoints = 3;
  46.                     }
  47.                     else
  48.                     {
  49.                         teamOnePoints = 1;
  50.                         teamTwoPoints = 1;
  51.                     }
  52.  
  53.                     if (!standings.ContainsKey(teamOne))
  54.                     {
  55.                         standings[teamOne] = 0;
  56.                     }
  57.  
  58.                     if (!standings.ContainsKey(teamTwo))
  59.                     {
  60.                         standings[teamTwo] = 0;
  61.                     }
  62.  
  63.                     standings[teamOne] += teamOnePoints;
  64.                     standings[teamTwo] += teamTwoPoints;
  65.  
  66.                     if (!goals.ContainsKey(teamOne))
  67.                     {
  68.                         goals[teamOne] = 0;
  69.                     }
  70.  
  71.                     if (!goals.ContainsKey(teamTwo))
  72.                     {
  73.                         goals[teamTwo] = 0;
  74.                     }
  75.  
  76.                     goals[teamOne] += teamOneGoals;
  77.                     goals[teamTwo] += teamTwoGoals;
  78.                 }
  79.                 input = Console.ReadLine();
  80.             }
  81.             var indexer = 1;
  82.             Console.WriteLine("League standings:");
  83.             foreach (var team in standings.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  84.             {                
  85.                 Console.WriteLine($"{indexer}. {team.Key} {team.Value}");
  86.                 indexer++;
  87.             }
  88.  
  89.             Console.WriteLine("Top 3 scored goals:");
  90.             foreach (var goal in goals.OrderByDescending(x => x.Value).ThenBy(x => x.Key).Take(3))
  91.             {
  92.                 Console.WriteLine($"- {goal.Key} -> {goal.Value}");
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement