Advertisement
krasizorbov

Ranking

Mar 16th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Rank
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //contest.Key and password.Value
  12.             var contestPassword = new Dictionary<string, string>();
  13.             //username.Key and dictionary.Value
  14.             var users = new Dictionary<string, Dictionary<string, int>>();
  15.  
  16.             while (true)
  17.             {
  18.                 string[] input = Console.ReadLine().Split(":").ToArray();
  19.  
  20.                 if (input[0] == "end of contests")
  21.                 {
  22.                     break;
  23.                 }
  24.  
  25.                 string contest = input[0];
  26.                 string password = input[1];
  27.  
  28.                 contestPassword.Add(contest, password);
  29.             }
  30.  
  31.             while (true)
  32.             {
  33.                 string[] input = Console.ReadLine().Split("=>").ToArray();
  34.  
  35.                 if (input[0] == "end of submissions")
  36.                 {
  37.                     break;
  38.                 }
  39.  
  40.                 string contest = input[0];
  41.                 string password = input[1];
  42.                 string username = input[2];
  43.                 int points = int.Parse(input[3]);
  44.  
  45.                 if (contestPassword.ContainsKey(contest) && contestPassword[contest] == password)
  46.                 {
  47.                     if (!users.ContainsKey(username))
  48.                     {
  49.                         users.Add(username, new Dictionary<string, int>());
  50.                         users[username][contest] = points;
  51.                     }
  52.                     else if (!users[username].ContainsKey(contest))   //check for the "contest" in the dictionary inside dictioary if it doesn't exist
  53.                     {
  54.                         users[username][contest] = points;
  55.                     }
  56.                     else if (users[username].ContainsKey(contest))    //check for the "contest" in the dictionary inside dictioary if it exists.
  57.                     {
  58.                         if (users[username][contest] < points)
  59.                         {
  60.                             users[username][contest] = points;
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             var namesAndPoints = new Dictionary<string, int>();    //Create new dictionary where "Name" is Key and "Max Sum" is Value.
  67.  
  68.             foreach (var name in users)
  69.             {
  70.                 namesAndPoints.Add(name.Key, name.Value.Values.Sum());    //Add to the new dictionary.
  71.             }
  72.  
  73.             //Print.
  74.             foreach (var name in namesAndPoints.OrderByDescending(x => x.Value))
  75.             {
  76.                 Console.WriteLine($"Best candidate is {name.Key} with total {name.Value} points.");
  77.  
  78.                 break;
  79.             }
  80.  
  81.             Console.WriteLine("Ranking: ");
  82.  
  83.             foreach (var name in users.OrderBy(x => x.Key))
  84.             {
  85.                 Console.WriteLine(name.Key);
  86.  
  87.                 foreach (var contest in name.Value.OrderByDescending(x => x.Value))
  88.                 {
  89.                     Console.WriteLine($"#  {contest.Key} -> {contest.Value}");
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement