yanchevilian

08. Ranking / C# Advanced / Dictionary

Jul 27th, 2021 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 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.             // Reading input from the console //
  12.             string inputInformation = Console.ReadLine();
  13.  
  14.             // Creating some structures of information //
  15.             Dictionary<string, string> allContests = new Dictionary<string, string>();
  16.             Dictionary<string, Dictionary<string, int>> usersWithContests =
  17.                 new Dictionary<string, Dictionary<string, int>>();
  18.            
  19.             // Fill in first Dictionary//
  20.             while (inputInformation?.ToLower() != "end of contests")
  21.             {
  22.                 string[] inputArr = inputInformation.Split(":", StringSplitOptions.RemoveEmptyEntries);
  23.                 string contest = inputArr[0];
  24.                 string passwordForContest = inputArr[1];
  25.  
  26.                 if (allContests.ContainsKey(contest) == false)
  27.                 {
  28.                     allContests.Add(contest,passwordForContest);
  29.                 }
  30.  
  31.                 inputInformation = Console.ReadLine();
  32.             }
  33.             // Reading information for users and their contests //
  34.             string usernameAndPassword = Console.ReadLine();
  35.  
  36.             // Fill in second Dictionary //
  37.             while (usernameAndPassword?.ToLower() != "end of submissions")
  38.             {
  39.                 string[] usernameAndPasswordArr =
  40.                     usernameAndPassword.Split("=>", StringSplitOptions.RemoveEmptyEntries);
  41.                 string contest = usernameAndPasswordArr[0];
  42.                 string password = usernameAndPasswordArr[1];
  43.                 string username = usernameAndPasswordArr[2];
  44.                 int points = int.Parse(usernameAndPasswordArr[3]);
  45.  
  46.                 // LOGIC (Happy Path statement) //
  47.                 if (allContests.ContainsKey(contest) && allContests[contest].Contains(password))
  48.                 {
  49.                     if (usersWithContests.ContainsKey(username) && usersWithContests[username].ContainsKey(contest))
  50.                     {
  51.                         // Check if the new points of current contest are bigger than older and if they are -> set them for new points//
  52.                         if (usersWithContests[username][contest] < points )
  53.                         {
  54.                             usersWithContests[username][contest] = points;
  55.                         }
  56.                     }
  57.                     else
  58.                     {
  59.                         if (usersWithContests.ContainsKey(username) == false)
  60.                         {
  61.                             usersWithContests.Add(username, new Dictionary<string, int>());
  62.                             usersWithContests[username].Add(contest, points);
  63.                         }
  64.                         else
  65.                         {
  66.                             usersWithContests[username].Add(contest, points);
  67.                         }
  68.                     }
  69.                 }
  70.                 usernameAndPassword = Console.ReadLine();
  71.             }
  72.  
  73.             string personWithBestScore = "";
  74.             int bestSum = int.MinValue;
  75.  
  76.             //Finding the person's name and his/her best score//
  77.             foreach (var user in usersWithContests)
  78.             {
  79.                 int maxSum = 0;
  80.                 foreach (var userValues in user.Value.Values)
  81.                 {
  82.                     int currentPersonSum = userValues;
  83.                     maxSum += currentPersonSum;
  84.  
  85.                     if (maxSum > bestSum)
  86.                     {
  87.                         bestSum = maxSum;
  88.                         personWithBestScore = user.Key;
  89.                     }
  90.                 }
  91.             }
  92.             // PRINTING WITH FOREEACH //
  93.             Console.WriteLine($"Best candidate is {personWithBestScore} with total {bestSum} points.");
  94.             Console.WriteLine("Ranking: ");
  95.             foreach (var users in usersWithContests.OrderBy(x => x.Key))
  96.             {
  97.                 Console.WriteLine($"{users.Key}");
  98.                 foreach (var values in users.Value.OrderByDescending(c => c.Value))
  99.                 {
  100.                     Console.WriteLine($"#  {values.Key} -> {values.Value}");
  101.                 }
  102.             }
  103.         }
  104.     }
  105. }
  106.  
Add Comment
Please, Sign In to add comment