Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _08._Ranking
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Reading input from the console //
- string inputInformation = Console.ReadLine();
- // Creating some structures of information //
- Dictionary<string, string> allContests = new Dictionary<string, string>();
- Dictionary<string, Dictionary<string, int>> usersWithContests =
- new Dictionary<string, Dictionary<string, int>>();
- // Fill in first Dictionary//
- while (inputInformation?.ToLower() != "end of contests")
- {
- string[] inputArr = inputInformation.Split(":", StringSplitOptions.RemoveEmptyEntries);
- string contest = inputArr[0];
- string passwordForContest = inputArr[1];
- if (allContests.ContainsKey(contest) == false)
- {
- allContests.Add(contest,passwordForContest);
- }
- inputInformation = Console.ReadLine();
- }
- // Reading information for users and their contests //
- string usernameAndPassword = Console.ReadLine();
- // Fill in second Dictionary //
- while (usernameAndPassword?.ToLower() != "end of submissions")
- {
- string[] usernameAndPasswordArr =
- usernameAndPassword.Split("=>", StringSplitOptions.RemoveEmptyEntries);
- string contest = usernameAndPasswordArr[0];
- string password = usernameAndPasswordArr[1];
- string username = usernameAndPasswordArr[2];
- int points = int.Parse(usernameAndPasswordArr[3]);
- // LOGIC (Happy Path statement) //
- if (allContests.ContainsKey(contest) && allContests[contest].Contains(password))
- {
- if (usersWithContests.ContainsKey(username) && usersWithContests[username].ContainsKey(contest))
- {
- // Check if the new points of current contest are bigger than older and if they are -> set them for new points//
- if (usersWithContests[username][contest] < points )
- {
- usersWithContests[username][contest] = points;
- }
- }
- else
- {
- if (usersWithContests.ContainsKey(username) == false)
- {
- usersWithContests.Add(username, new Dictionary<string, int>());
- usersWithContests[username].Add(contest, points);
- }
- else
- {
- usersWithContests[username].Add(contest, points);
- }
- }
- }
- usernameAndPassword = Console.ReadLine();
- }
- string personWithBestScore = "";
- int bestSum = int.MinValue;
- //Finding the person's name and his/her best score//
- foreach (var user in usersWithContests)
- {
- int maxSum = 0;
- foreach (var userValues in user.Value.Values)
- {
- int currentPersonSum = userValues;
- maxSum += currentPersonSum;
- if (maxSum > bestSum)
- {
- bestSum = maxSum;
- personWithBestScore = user.Key;
- }
- }
- }
- // PRINTING WITH FOREEACH //
- Console.WriteLine($"Best candidate is {personWithBestScore} with total {bestSum} points.");
- Console.WriteLine("Ranking: ");
- foreach (var users in usersWithContests.OrderBy(x => x.Key))
- {
- Console.WriteLine($"{users.Key}");
- foreach (var values in users.Value.OrderByDescending(c => c.Value))
- {
- Console.WriteLine($"# {values.Key} -> {values.Value}");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment