Teo_Yanchev

Dictionaries - 01. Ranking - C# Fundamentals

Oct 6th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5.  
  6. namespace AssociativeArrays__Dictionary_____MoreExercise
  7. {
  8. class Ranking
  9. {
  10. static void Main()
  11. {
  12. Dictionary<string, string> contestsAndPasswords = new Dictionary<string, string>();
  13.  
  14. Dictionary<string, Dictionary<string, int>> interns = new Dictionary<string, Dictionary<string, int>>();
  15.  
  16. string commandOne = Console.ReadLine();
  17. while (commandOne != "end of contests")
  18. {
  19. string[] currentCommand = commandOne.Split(':');
  20. string contest = currentCommand[0];
  21. string password = currentCommand[1];
  22.  
  23. if (!contestsAndPasswords.ContainsKey(contest))
  24. {
  25. contestsAndPasswords[contest] = password;
  26. }
  27.  
  28. commandOne = Console.ReadLine();
  29. }
  30.  
  31. string commandTwo = Console.ReadLine();
  32. while (commandTwo != "end of submissions")
  33. {
  34. string[] input = commandTwo.Split("=>");
  35. string contest = input[0];
  36. string password = input[1];
  37. string userName = input[2];
  38. int points = int.Parse(input[3]);
  39.  
  40. bool currentContest = contestsAndPasswords.ContainsKey(contest);
  41. bool currentPassword = contestsAndPasswords.ContainsValue(password);
  42.  
  43. if (currentContest && currentPassword)
  44. {
  45. if (!interns.ContainsKey(userName))
  46. {
  47. interns[userName] = new Dictionary<string, int>();
  48.  
  49. }
  50.  
  51. if (!interns[userName].ContainsKey(contest))
  52. {
  53. interns[userName].Add(contest, points);
  54. }
  55. else
  56. {
  57. if (interns[userName].ContainsKey(contest) && interns[userName][contest] < points)
  58. {
  59. interns[userName][contest] = points;
  60.  
  61. }
  62. }
  63.  
  64. }
  65.  
  66. commandTwo = Console.ReadLine();
  67. }
  68.  
  69. string bestName = string.Empty;
  70. int bestSum = 0;
  71.  
  72. foreach (var candidate in interns)
  73. {
  74. string currentNameCandidate = candidate.Key;
  75. Dictionary<string, int> currentBestPoints = interns[currentNameCandidate];
  76.  
  77. int sum = 0;
  78. string name = string.Empty;
  79. foreach (var kvp in currentBestPoints)
  80. {
  81. string nameContest = kvp.Key;
  82. int points = kvp.Value;
  83.  
  84. sum += points;
  85. name = nameContest;
  86. }
  87. if (sum > bestSum)
  88. {
  89. bestName = currentNameCandidate;
  90. bestSum = sum;
  91. }
  92. }
  93.  
  94. Console.WriteLine($"Best candidate is {bestName} with total {bestSum} points.");
  95. Console.WriteLine("Ranking: ");
  96.  
  97. interns = interns
  98. .OrderBy(x => x.Key)
  99. .ToDictionary(x => x.Key, x => x.Value);
  100.  
  101. foreach (var kvp in interns)
  102. {
  103. string name = kvp.Key;
  104. Dictionary<string, int> contestAndPoints = interns[name];
  105.  
  106.  
  107. contestAndPoints = contestAndPoints
  108. .OrderByDescending(x => x.Value)
  109. .ToDictionary(x => x.Key, x => x.Value);
  110.  
  111. Console.WriteLine($"{name}");
  112. foreach (var kvp2 in contestAndPoints)
  113. {
  114. string contest = kvp2.Key;
  115. int points = kvp2.Value;
  116.  
  117. Console.WriteLine($"# {contest} -> {points}");
  118. }
  119. }
  120. }
  121. }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment