Advertisement
Guest User

FootballLeague_strings

a guest
Oct 21st, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. class MainClass
  8. {
  9.     public static void Main()
  10.     {
  11.         string key = Console.ReadLine();
  12.         string input = Console.ReadLine();
  13.  
  14.         var standings = new Dictionary<string, int>();  //for the points gained
  15.         var goals = new Dictionary<string, int>();     //for the number of goals scored
  16.  
  17.         while (!input.ToLower().Equals("final"))
  18.         {
  19.             var args = input.Split(new[] { ' ', ':' }, StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.             string encryptedTeamA = args[0];               
  22.             string encryptedTeamB = args[1];               
  23.  
  24.             int teamAGoals = int.Parse(args[2]);
  25.             int teamBGoals = int.Parse(args[3]);
  26.  
  27.             var teamANameChars = encryptedTeamA.Skip(encryptedTeamA.IndexOf(key) + key.Length).ToArray();
  28.             encryptedTeamA = string.Join("", teamANameChars);
  29.             string teamAName = string.Join("", teamANameChars.Take(encryptedTeamA.IndexOf(key)).Reverse()).ToUpper();
  30.  
  31.             var teamBNameChars = encryptedTeamB.Skip(encryptedTeamB.IndexOf(key) + key.Length).ToArray();
  32.             encryptedTeamB = string.Join("", teamBNameChars);
  33.             string teamBName = string.Join("", teamBNameChars.Take(encryptedTeamB.IndexOf(key)).Reverse()).ToUpper();
  34.  
  35.             InsertInStandings(standings, teamAName, teamBName, teamAGoals, teamBGoals);
  36.             InsertInGoals(goals, teamAName, teamBName, teamAGoals, teamBGoals);
  37.  
  38.             input = Console.ReadLine();
  39.         }
  40.  
  41.         PrintStandings(standings);
  42.         PrintBest3Teams(goals);
  43.     }
  44.  
  45.     static void PrintStandings(Dictionary<string, int> standings)
  46.     {
  47.         Console.WriteLine("League standings:");
  48.         int counter = 1;
  49.  
  50.         foreach (var team in standings.OrderByDescending(t => t.Value).ThenBy(t => t.Key))
  51.         {
  52.             Console.WriteLine($"{counter}. {team.Key} {team.Value}");
  53.             counter++;
  54.         }
  55.     }
  56.  
  57.     static void PrintBest3Teams(Dictionary<string, int> goals)
  58.     {
  59.         Console.WriteLine("Top 3 scored goals:");
  60.  
  61.         foreach (var team in goals.OrderByDescending(t => t.Value).ThenBy(t => t.Key).Take(3))
  62.         {
  63.             Console.WriteLine($"- {team.Key} -> {team.Value}");
  64.         }
  65.  
  66.     }
  67.  
  68.     static void InsertInStandings(Dictionary<string, int> standings, string teamAName, string teamBName, int teamAGoals, int teamBGoals)
  69.     {
  70.         if (!standings.ContainsKey(teamAName))
  71.             standings.Add(teamAName, 0);
  72.  
  73.         if (!standings.ContainsKey(teamBName))
  74.             standings.Add(teamBName, 0);
  75.  
  76.         int teamAPoints = 1;
  77.         int teamBPoints = 1;
  78.  
  79.         if (teamBGoals != teamAGoals)
  80.         {
  81.             teamAPoints = (teamAGoals > teamBGoals) ? 3 : 0;
  82.             teamBPoints = (teamBGoals > teamAGoals) ? 3 : 0;
  83.         }
  84.  
  85.         standings[teamAName] += teamAPoints;
  86.         standings[teamBName] += teamBPoints;
  87.     }
  88.  
  89.     static void InsertInGoals(Dictionary<string, int> goals, string teamAName, string teamBName, int teamAGoals, int teamBGoals)
  90.     {
  91.         if (!goals.ContainsKey(teamAName))
  92.             goals.Add(teamAName, 0);
  93.  
  94.         if (!goals.ContainsKey(teamBName))
  95.             goals.Add(teamBName, 0);
  96.  
  97.         goals[teamAName] += teamAGoals;
  98.         goals[teamBName] += teamBGoals;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement