Advertisement
alexbancheva

Ranking_Sets_and_Dictionaries

Mar 30th, 2021
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Design;
  4. using System.Linq;
  5.  
  6. namespace Ranking_Sets_and_Dictionaries
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Dictionary<string, string> contestInfo = new Dictionary<string, string>();
  13.             string input = Console.ReadLine();
  14.  
  15.             while (input != "end of contests")
  16.             {
  17.                 string[] data = input.Split(":");
  18.                 string contest = data[0];
  19.                 string passContest = data[1];
  20.  
  21.                 contestInfo.Add(contest, passContest);
  22.                 input = Console.ReadLine();
  23.             }
  24.  
  25.             Dictionary<string, Dictionary<string, int>> usersInfo = new Dictionary<string, Dictionary<string, int>>();
  26.  
  27.             string info = Console.ReadLine();
  28.  
  29.             while (info != "end of submissions")
  30.             {
  31.                 string[] data = info.Split("=>");
  32.                 string contest = data[0];
  33.                 string password = data[1];
  34.                 string username = data[2];
  35.                 int points = int.Parse(data[3]);
  36.  
  37.                 if (contestInfo.ContainsKey(contest))
  38.                 {
  39.                     if (contestInfo[contest] == password)
  40.                     {
  41.  
  42.                         if (!usersInfo.ContainsKey(username))
  43.                         {
  44.                             usersInfo.Add(username, new Dictionary<string, int>());
  45.                         }
  46.  
  47.                         if (!usersInfo[username].ContainsKey(contest))
  48.                         {
  49.                             usersInfo[username].Add(contest, points);
  50.                         }
  51.                         else
  52.                         {
  53.                             if (usersInfo[username][contest] < points)
  54.                             {
  55.                                 usersInfo[username][contest] = points;
  56.                             }
  57.                         }
  58.                     }
  59.                 }
  60.                 info = Console.ReadLine();
  61.             }
  62.  
  63.             KeyValuePair<string, int> bestCandidate = BestCandidate(usersInfo);
  64.             Console.WriteLine($"Best candidate is {bestCandidate.Key} with total {bestCandidate.Value} points.");
  65.             Console.WriteLine("Ranking:");
  66.  
  67.             foreach (var student in usersInfo.OrderBy(s=>s.Key))
  68.             {
  69.                 Console.WriteLine(student.Key);
  70.  
  71.                 foreach (var contest in student.Value.OrderByDescending(p=>p.Value))
  72.                 {
  73.                     Console.WriteLine($"#  {contest.Key} -> {contest.Value}");
  74.                 }
  75.             }
  76.  
  77.         }
  78.         private static KeyValuePair<string, int> BestCandidate(Dictionary<string, Dictionary<string, int>> usersInfo)
  79.         {
  80.             int bestPoints = 0;
  81.             string bestCand = string.Empty;
  82.  
  83.             foreach (var (key, value) in usersInfo)
  84.             {
  85.                 string username = key;
  86.                 int sumPoints = value.Values.Sum();
  87.  
  88.                 if (bestPoints < sumPoints)
  89.                 {
  90.                     bestPoints = sumPoints;
  91.                     bestCand = username;
  92.                 }
  93.             }
  94.             KeyValuePair<string, int> bestCandidate = new KeyValuePair<string, int>(bestCand, bestPoints);
  95.             return bestCandidate;
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement