Advertisement
bullit3189

Ranking - Dictionaries

Jan 24th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. public class Program
  7. {
  8. public static void Main()
  9. {
  10. Dictionary <string, Dictionary<string,int>> userContestPoints = new Dictionary<string, Dictionary<string,int>>();
  11. Dictionary <string,string> contestPass = new Dictionary<string,string>();
  12.  
  13. while (true)
  14. {
  15. string command = Console.ReadLine();
  16.  
  17. if (command == "end of contests")
  18. {
  19. break;
  20. }
  21.  
  22. string[] tokens = command.Split(':');
  23.  
  24. string contest = tokens[0];
  25. string pass = tokens[1];
  26.  
  27. if (!contestPass.ContainsKey(contest))
  28. {
  29. contestPass.Add(contest,pass);
  30. }
  31. }
  32.  
  33. while (true)
  34. {
  35. string command = Console.ReadLine();
  36.  
  37. if (command == "end of submissions")
  38. {
  39. break;
  40. }
  41.  
  42. string[] tokens = command.Split("=>");
  43. string contest = tokens[0];
  44. string pass = tokens[1];
  45. string username = tokens[2];
  46. int points = int.Parse(tokens[3]);
  47.  
  48. if (contestPass.ContainsKey(contest))
  49. {
  50. if (contestPass[contest]==pass)
  51. {
  52. if (!userContestPoints.ContainsKey(username))
  53. {
  54. userContestPoints.Add(username,new Dictionary<string,int>());
  55. userContestPoints[username].Add(contest,points);
  56. }
  57. else if (userContestPoints.ContainsKey(username)==true && userContestPoints[username].ContainsKey(contest)==false)
  58. {
  59. userContestPoints[username].Add(contest,points);
  60. }
  61. else if (userContestPoints.ContainsKey(username)==true && userContestPoints[username].ContainsKey(contest)==true)
  62. {
  63. if (points> userContestPoints[username][contest])
  64. {
  65. userContestPoints[username][contest] =points;
  66. }
  67. }
  68. }
  69. }
  70. }
  71.  
  72. string bestUser = string.Empty;
  73. int bestPoints =0;
  74. int pointsTotal=0;
  75.  
  76. foreach (var kvp in userContestPoints)
  77. {
  78. string name = kvp.Key;
  79.  
  80. foreach (var inner in kvp.Value)
  81. {
  82. pointsTotal += inner.Value;
  83. }
  84. if (pointsTotal>bestPoints)
  85. {
  86. bestPoints = pointsTotal;
  87. bestUser = name;
  88. }
  89. pointsTotal=0;
  90. }
  91. Console.WriteLine("Best candidate is {0} with total {1} points.",bestUser,bestPoints);
  92. Console.WriteLine("Ranking:");
  93.  
  94. foreach (var currKvp in userContestPoints.OrderBy(x=>x.Key))
  95. {
  96. Console.WriteLine(currKvp.Key);
  97.  
  98. foreach (var innerKvp in currKvp.Value.OrderByDescending(x=>x.Value))
  99. {
  100. Console.WriteLine("# {0} -> {1}",innerKvp.Key,innerKvp.Value);
  101. }
  102. }
  103.  
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement