Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace FootballLeague
- {
- class Program
- {
- public static Regex teams;
- static void Main(string[] args)
- {
- string enteredPattern = Console.ReadLine();
- StringBuilder pattern = EscapeTheCharsInThePatt(enteredPattern);
- try
- {
- teams = new Regex(enteredPattern + @"(\w+)" + enteredPattern);
- }
- catch (Exception)
- {
- teams = new Regex(pattern.ToString() + @"(\w+)" + pattern.ToString());
- }
- Regex goals = new Regex(@"(\d+):(\d+)");
- Dictionary<string, long> teamsResults = new Dictionary<string, long>();
- Dictionary<string, long> top3ScoredGoals = new Dictionary<string, long>();
- string inputLine = Console.ReadLine();
- while (!inputLine.Equals("final"))
- {
- MatchCollection allTeams = teams.Matches(inputLine);
- string[] twoTeamsCompeting = new string[2];
- int index = 0;
- foreach (Match match in allTeams)
- {
- twoTeamsCompeting[index] = string.Join("", match.Groups[1].ToString().ToUpper().Reverse());
- index++;
- }
- Match results = goals.Match(inputLine);
- long[] teamsGoals = new long[2];
- teamsGoals[0] = long.Parse(results.Groups[1].ToString());
- teamsGoals[1] = long.Parse(results.Groups[2].ToString());
- for (int currTeam = 0; currTeam < twoTeamsCompeting.Length; currTeam++)
- {
- if (!top3ScoredGoals.ContainsKey(twoTeamsCompeting[currTeam]))
- {
- top3ScoredGoals[twoTeamsCompeting[currTeam]] = teamsGoals[currTeam];
- }
- else
- {
- top3ScoredGoals[twoTeamsCompeting[currTeam]] += teamsGoals[currTeam];
- }
- }
- //if first team wins
- if (teamsGoals[0] > teamsGoals[1])
- {
- //appending points to the winner
- if (!teamsResults.ContainsKey(twoTeamsCompeting[0]))
- {
- teamsResults.Add(twoTeamsCompeting[0], 3);
- }
- else
- {
- teamsResults[twoTeamsCompeting[0]] += 3;
- }
- //appending points to the loser
- if (!teamsResults.ContainsKey(twoTeamsCompeting[1]))
- {
- teamsResults.Add(twoTeamsCompeting[1], 0);
- }
- else
- {
- teamsResults[twoTeamsCompeting[1]] += 0;
- }
- }
- //if the second team wins
- else if (teamsGoals[0] < teamsGoals[1])
- {
- //appending points to the winner
- if (!teamsResults.ContainsKey(twoTeamsCompeting[1]))
- {
- teamsResults.Add(twoTeamsCompeting[1], 3);
- }
- else
- {
- teamsResults[twoTeamsCompeting[1]] += 3;
- }
- //appending points to the loser
- if (!teamsResults.ContainsKey(twoTeamsCompeting[0]))
- {
- teamsResults.Add(twoTeamsCompeting[0], 0);
- }
- else
- {
- teamsResults[twoTeamsCompeting[0]] += 0;
- }
- }
- //if the match is draw
- else
- {
- //appending points to the winner
- if (!teamsResults.ContainsKey(twoTeamsCompeting[0]))
- {
- teamsResults.Add(twoTeamsCompeting[0], 1);
- }
- else
- {
- teamsResults[twoTeamsCompeting[0]] += 1;
- }
- //appending points to the loser
- if (!teamsResults.ContainsKey(twoTeamsCompeting[1]))
- {
- teamsResults.Add(twoTeamsCompeting[1], 1);
- }
- else
- {
- teamsResults[twoTeamsCompeting[1]] += 1;
- }
- }
- inputLine = Console.ReadLine();
- }
- //print LeagueStandings
- Console.WriteLine("League standings:");
- int counterForPlaceInTable = 1;
- foreach (KeyValuePair<string, long> teamPointsPair in teamsResults
- .OrderByDescending(p => p.Value)
- .ThenBy(n => n.Key))
- {
- Console.WriteLine($"{counterForPlaceInTable}. {teamPointsPair.Key} {teamPointsPair.Value}");
- counterForPlaceInTable++;
- }
- //print top 3 goals
- Console.WriteLine("Top 3 scored goals:");
- foreach (KeyValuePair<string, long> topGoalsPair in top3ScoredGoals
- .OrderByDescending(p => p.Value)
- .ThenBy(n => n.Key).Take(3))
- {
- Console.WriteLine($"- {topGoalsPair.Key} -> {topGoalsPair.Value}");
- }
- }
- private static StringBuilder EscapeTheCharsInThePatt(string enteredPattern)
- {
- StringBuilder strToReturn = new StringBuilder();
- for (int currElement = 0; currElement < enteredPattern.Length; currElement++)
- {
- strToReturn.Append('\\');
- strToReturn.Append(enteredPattern[currElement]);
- }
- return strToReturn;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment