Advertisement
Guest User

08.Ranking

a guest
Oct 1st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _08.Ranking
  6. {
  7. class StartUp
  8. {
  9. public static Dictionary<string, Dictionary<string, int>> studentsSubmitions = new Dictionary<string, Dictionary<string, int>>();
  10. public static Dictionary<string, string> contestPasswords = new Dictionary<string, string>();
  11. static void Main(string[] args)
  12. {
  13.  
  14. GetContests();
  15.  
  16. GetSumbmitions();
  17.  
  18. PrintBestCandidate();
  19.  
  20. PrintCandidatesWithResults();
  21. }
  22.  
  23. private static void PrintCandidatesWithResults()
  24. {
  25. Console.WriteLine("Ranking:");
  26. foreach (var student in studentsSubmitions.OrderBy(e=>e.Key))
  27. {
  28. Console.WriteLine(student.Key);
  29. foreach (var courseResult in student.Value.OrderByDescending(e=>e.Value))
  30. {
  31. Console.WriteLine($"# {courseResult.Key} -> {courseResult.Value}");
  32. }
  33. }
  34. }
  35.  
  36. private static void PrintBestCandidate()
  37. {
  38. var student = studentsSubmitions.OrderByDescending(e => e.Value.Values.Sum()).FirstOrDefault();
  39. Console.WriteLine($"Best candidate is {student.Key} with total {student.Value.Values.Sum()} points.");
  40. }
  41.  
  42. private static void GetSumbmitions()
  43. {
  44. while (true)
  45. {
  46. var input = Console.ReadLine();
  47.  
  48. if (input == "end of submissions")
  49. {
  50. break;
  51. }
  52.  
  53. var submitionArgs = input.Split("=>");
  54.  
  55. var course = submitionArgs[0];
  56. var coursePassword = submitionArgs[1];
  57. var username = submitionArgs[2];
  58. var points = int.Parse(submitionArgs[3]);
  59.  
  60. bool isContestValid = IsValidContest(course, coursePassword);
  61. if (isContestValid)
  62. {
  63. SubmitResult(course, username, points);
  64. }
  65. }
  66. }
  67.  
  68. private static void SubmitResult(string course, string username, int points)
  69. {
  70. if (studentsSubmitions.ContainsKey(username))
  71. {
  72. if (studentsSubmitions[username].ContainsKey(course))
  73. {
  74. int previousValue = studentsSubmitions[username][course];
  75. if (previousValue<points)
  76. {
  77. studentsSubmitions[username][course] = points;
  78. }
  79. }
  80. else
  81. {
  82. studentsSubmitions[username].Add(course, points);
  83. }
  84. }
  85. else
  86. {
  87. studentsSubmitions.Add(username, new Dictionary<string, int>());
  88. studentsSubmitions[username].Add(course, points);
  89. }
  90. }
  91.  
  92. private static bool IsValidContest(string course, string coursePassword)
  93. {
  94. if (contestPasswords.ContainsKey(course))
  95. {
  96. if (contestPasswords[course] == coursePassword)
  97. {
  98. return true;
  99. }
  100. return false;
  101. }
  102. return false;
  103. }
  104.  
  105. private static void GetContests()
  106. {
  107. while (true)
  108. {
  109. var input = Console.ReadLine();
  110.  
  111. if (input == "end of contests")
  112. {
  113. break;
  114. }
  115. var contestArgs = input.Split(':');
  116. var name = contestArgs[0];
  117. var password = contestArgs[1];
  118.  
  119. if (!contestPasswords.ContainsKey(name))
  120. {
  121. contestPasswords.Add(name, password);
  122. }
  123. }
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement