Advertisement
GerganaTsirkova

Football standings

Mar 15th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _3.FootballStandigs
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var goalTeam = new Dictionary<string, long>();
  13.             var pointTeam = new Dictionary<string, long>();
  14.             string pattern = Console.ReadLine();
  15.            
  16.             while(true)
  17.             {
  18.                 string[] input = Console.ReadLine().Split(' ').ToArray();
  19.                 if(input[0]=="final")
  20.                 {
  21.                     break;
  22.                 }
  23.                 string teamFirst = TeamNameFinder(input[0].ToString(),pattern);
  24.                 string teamSecond= TeamNameFinder(input[1].ToString(), pattern);
  25.                 string[] goals = input[2].Split(':').ToArray();
  26.                 long firstTeamGoals = long.Parse(goals[0]);
  27.                 long secondTeamGoals = long.Parse(goals[1]);
  28.                 long firsTeamPoints = 0;
  29.                 long secondTeamPoints = 0;
  30.                 if(firstTeamGoals==secondTeamGoals)
  31.                 {
  32.                     firsTeamPoints++;
  33.                     secondTeamPoints++;
  34.                 }
  35.                 else if(firstTeamGoals>secondTeamGoals)
  36.                 {
  37.                     firsTeamPoints += 3;
  38.                 }
  39.                 else
  40.                 {
  41.                     secondTeamPoints += 3;
  42.                 }
  43.                 AddingPointsToKeyValuePairs(teamFirst, firsTeamPoints, firstTeamGoals,goalTeam,pointTeam);
  44.                 AddingPointsToKeyValuePairs(teamSecond, secondTeamPoints, secondTeamGoals,goalTeam,pointTeam);
  45.  
  46.             }
  47.             if(pointTeam.Count>0)
  48.             {
  49.                 Console.WriteLine("League standings:");
  50.                 int counter = 1;
  51.                 foreach (var item in pointTeam.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  52.                 {
  53.                     Console.WriteLine($"{counter}. {item.Key} {item.Value}");
  54.                     counter++;
  55.                 }
  56.             }
  57.             if(goalTeam.Count>0)
  58.             {
  59.                 Console.WriteLine("Top 3 scored goals:");
  60.                 int count = 0;
  61.                 foreach (var item in goalTeam.OrderByDescending(x => x.Value))
  62.                 {
  63.                     Console.WriteLine($"- {item.Key} -> {item.Value}");
  64.                     count++;
  65.                     if (count == 3)
  66.                     {
  67.                         break;
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.  
  73.         static void AddingPointsToKeyValuePairs(string team, long teamPoints, long teamGoals, Dictionary<string, long> goalTeam, Dictionary<string, long> pointTeam)
  74.         {
  75.             if(!goalTeam.ContainsKey(team))
  76.             {
  77.                 goalTeam.Add(team, teamGoals);
  78.             }
  79.             else if(goalTeam.ContainsKey(team))
  80.             {
  81.                 goalTeam[team] += teamGoals;
  82.             }
  83.             if(!pointTeam.ContainsKey(team))
  84.             {
  85.                 pointTeam.Add(team, teamPoints);
  86.             }
  87.             else if(pointTeam.ContainsKey(team))
  88.             {
  89.                 pointTeam[team] += teamPoints;
  90.             }
  91.         }
  92.  
  93.         static string TeamNameFinder(string text, string pattern)
  94.         {
  95.             var unreversed = new StringBuilder();
  96.             int start = text.IndexOf(pattern, 0) + pattern.Length;
  97.             int end = text.IndexOf(pattern, start);
  98.             string name = "";
  99.             for (int i = start; i < end; i++)
  100.             {
  101.                 unreversed.Append(text[i]);
  102.             }
  103.             for (int i = unreversed.Length - 1; i >= 0; i--)
  104.             {
  105.                 name += unreversed[i];
  106.             }
  107.             name = name.ToUpper();
  108.             return name;
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement