Advertisement
Guest User

03. Football Standingd

a guest
Jun 13th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class FootballLeague
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         Dictionary<string, int> teamsPoints = new Dictionary<string, int>();
  12.         Dictionary<string, int> teamsGoals = new Dictionary<string, int>();
  13.  
  14.         ReadData(teamsPoints, teamsGoals);
  15.         PrintResults(teamsPoints, teamsGoals);
  16.  
  17.     }
  18.  
  19.     private static void PrintResults(Dictionary<string, int> teamsPoints, Dictionary<string, int> teamsGoals)
  20.     {
  21.         Console.WriteLine("League standings:");
  22.         var sorted = teamsPoints.OrderByDescending(points => points.Value).ThenBy(name => name.Key);
  23.         int position = 1;
  24.         foreach (var country in sorted)
  25.         {
  26.             string team = country.Key.ToUpper();
  27.             int points = country.Value;
  28.             Console.WriteLine($"{position}. {team} {points}");
  29.             position++;
  30.         }
  31.  
  32.         Console.WriteLine("Top 3 scored goals:");
  33.         var top3Teams = teamsGoals.OrderByDescending(goals => goals.Value).ThenBy(name => name.Key).Take(3);
  34.         foreach (var country in top3Teams)
  35.         {
  36.             string team = country.Key.ToUpper();
  37.             int goals = country.Value;
  38.             Console.WriteLine($"- {team} -> {goals}");
  39.         }
  40.     }
  41.  
  42.     private static void ReadData(Dictionary<string, int> teamsPoints, Dictionary<string, int> teamsGoals)
  43.     {
  44.         string delimiter = Console.ReadLine();
  45.         while (true)
  46.         {
  47.             var line = Console.ReadLine();
  48.             if (line == "final") break;
  49.  
  50.             string[] info = ExtractInfo(line, delimiter);
  51.  
  52.             string firstTeam = Reverse(info[0]);
  53.             string secondTeam = Reverse(info[1]);
  54.  
  55.             int[] results = info[2]
  56.                 .Split(':')
  57.                 .Select(int.Parse)
  58.                 .ToArray();
  59.  
  60.             int firstTeamGoals = results[0];
  61.             int secondTeamGoals = results[1];
  62.  
  63.             FillDataInTables(teamsPoints, teamsGoals, firstTeam, firstTeamGoals);
  64.             FillDataInTables(teamsPoints, teamsGoals, secondTeam, secondTeamGoals);
  65.  
  66.             if (firstTeamGoals > secondTeamGoals)
  67.             {
  68.                 teamsPoints[firstTeam] += 3;
  69.             }
  70.             else if (firstTeamGoals == secondTeamGoals)
  71.             {
  72.                 teamsPoints[firstTeam]++;
  73.                 teamsPoints[secondTeam]++;
  74.             }
  75.             else
  76.             {
  77.                 teamsPoints[secondTeam] += 3;
  78.             }
  79.         }
  80.     }
  81.  
  82.     private static void FillDataInTables(Dictionary<string, int> teamsPoints, Dictionary<string, int> teamsGoals, string team, int goals)
  83.     {
  84.         if (!teamsPoints.ContainsKey(team))
  85.         {
  86.             teamsPoints.Add(team, 0);
  87.         }
  88.  
  89.         if (!teamsGoals.ContainsKey(team))
  90.         {
  91.             teamsGoals.Add(team, goals);
  92.         }
  93.         else
  94.         {
  95.             teamsGoals[team] += goals;
  96.         }
  97.     }
  98.  
  99.     private static string Reverse(string name)
  100.     {
  101.         StringBuilder text = new StringBuilder();
  102.  
  103.         for (int i = name.Length - 1; i >= 0; i--)
  104.         {
  105.             text.Append(name[i]);
  106.         }
  107.  
  108.         return text.ToString().ToLower();
  109.     }
  110.  
  111.     private static string[] ExtractInfo(string line, string delimiter)
  112.     {
  113.         string[] info = new string[3];
  114.  
  115.         string[] split = line
  116.             .Split(new string[] { " "}, StringSplitOptions.RemoveEmptyEntries)
  117.             .ToArray();
  118.  
  119.         info[0] = extractData(split[0], delimiter);
  120.         info[1] = extractData(split[1], delimiter);
  121.         info[2] = split[2];  
  122.  
  123.         return info;
  124.     }
  125.  
  126.     private static string extractData(string p, string delimiter)
  127.     {        
  128.         int startIndex = p.IndexOf(delimiter) + delimiter.Length;
  129.         int length = p.IndexOf(delimiter, startIndex) - startIndex;
  130.  
  131.         string data = p.Substring(startIndex, length);
  132.  
  133.         return data;
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement