Advertisement
dimitaryodimitrov

C# Advanced, Sets and Dictionaries

Sep 28th, 2021
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _08.Ranking
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var contestData = new Dictionary<string, string>();
  12.  
  13.             var line = Console.ReadLine();
  14.  
  15.             while (line != "end of contests")
  16.             {
  17.                 var parts = line
  18.                     .Split(":", StringSplitOptions.RemoveEmptyEntries)
  19.                     .ToArray();
  20.  
  21.                 string contest = parts[0];
  22.                 string password = parts[1];
  23.  
  24.                 if (!contestData.ContainsKey(contest))
  25.                 {
  26.                     contestData.Add(contest, password);
  27.                 }
  28.  
  29.                 line = Console.ReadLine();
  30.             }
  31.  
  32.             line = Console.ReadLine();
  33.  
  34.             var usersData = new Dictionary<string, Dictionary<string, int>>();
  35.  
  36.             while (line != "end of submissions")
  37.             {
  38.                 var parts = line
  39.                     .Split("=>", StringSplitOptions.RemoveEmptyEntries)
  40.                     .ToArray();
  41.  
  42.                 string contest = parts[0];
  43.                 string password = parts[1];
  44.                 string userName = parts[2];
  45.                 int points = int.Parse(parts[3]);
  46.  
  47.                 if (contestData.ContainsKey(contest) && contestData[contest] == password)
  48.                 {
  49.                     if (!usersData.ContainsKey(userName))
  50.                     {
  51.                         usersData.Add(userName, new Dictionary<string, int>());
  52.                     }
  53.  
  54.                     if (!usersData[userName].ContainsKey(contest))
  55.                     {
  56.                         usersData[userName][contest] = points;
  57.                     }
  58.  
  59.                     if (usersData[userName][contest] < points)
  60.                     {
  61.                         usersData[contest][contest] = points;
  62.                     }
  63.                 }
  64.  
  65.                 line = Console.ReadLine();
  66.             }
  67.  
  68.             string bestStudent = string.Empty;
  69.             int topSum = int.MinValue;
  70.  
  71.             foreach (var user in usersData)
  72.             {
  73.                 var studentsPoints = 0;
  74.  
  75.                 foreach (var point in user.Value)
  76.                 {
  77.                     studentsPoints += point.Value;
  78.                 }
  79.  
  80.                 if (studentsPoints > topSum)
  81.                 {
  82.                     topSum = studentsPoints;
  83.                     bestStudent = user.Key;
  84.                 }
  85.             }
  86.  
  87.             Console.WriteLine($"Best candidate is {bestStudent} with total {topSum} points.");
  88.             Console.WriteLine("Ranking:");
  89.  
  90.             foreach (var user in usersData.OrderBy(u => u.Key))
  91.             {
  92.                 Console.WriteLine(user.Key);
  93.  
  94.                 foreach (var contest in user.Value.OrderByDescending(c => c.Value))
  95.                 {
  96.                     Console.WriteLine($"#  {contest.Key} -> {contest.Value}");
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement