Advertisement
bodyquest

Ranking- PFund-Sept2018 - Dict, LINQ,Lambda

Dec 2nd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MoreExercises_1_Ranking
  9. {
  10. class Program
  11. {
  12. static void Main()
  13. {
  14. //ranking according to POINTS from interview tasks
  15. // and from POINTS from exams in SoftUni
  16.  
  17. Dictionary<string, string> contestPass = new Dictionary<string, string>();
  18.  
  19.  
  20. while (true)
  21. {
  22. string contest = Console.ReadLine();
  23. string[] array = new string[2];
  24. if (contest == "end of contests")
  25. {
  26. break;
  27. }
  28.  
  29. array = contest.Split(':').ToArray();
  30. string contestName = array[0];
  31. string password = array[1];
  32.  
  33. if (contestPass.ContainsKey(contestName) == false)
  34. {
  35. contestPass[contestName] = password;
  36. }
  37. }
  38.  
  39. var namePoints = new Dictionary<string, int>();
  40. var userContestPoints = new Dictionary<string, Dictionary<string, int>>();
  41.  
  42. while (true)
  43. {
  44. string input = Console.ReadLine();
  45. string[] arraySubmissions = new string[4];
  46. if (input == "end of submissions")
  47. {
  48. break;
  49. }
  50.  
  51. arraySubmissions = Regex.Split(input, "=>").ToArray();
  52. string contestName = arraySubmissions[0];
  53. string password = arraySubmissions[1];
  54. string username = arraySubmissions[2];
  55. string points = arraySubmissions[3];
  56.  
  57. if (contestPass.ContainsKey(contestName) == true && contestPass.ContainsValue(password) == true)
  58. {
  59.  
  60. if (!userContestPoints.ContainsKey(username))
  61. {
  62. namePoints[username] = int.Parse(arraySubmissions[3]);
  63. userContestPoints[username] = new Dictionary<string, int>();
  64. var userContestResults = userContestPoints[username];
  65. userContestResults[contestName] = int.Parse(arraySubmissions[3]);
  66. }
  67. else
  68. {
  69. namePoints[username] += int.Parse(arraySubmissions[3]);
  70. var userContestResults = userContestPoints[username];
  71. userContestResults[contestName] = int.Parse(arraySubmissions[3]);
  72. }
  73. }
  74.  
  75. //else
  76. //{
  77. //// if (userContestPoints.Values.Where(d=>d.ContainsValue(key)))
  78. //// {
  79. //// var query = test.Values // select inner dictionaries
  80. ////.Where(d => d.ContainsKey(key))
  81. //// }
  82.  
  83. //}
  84.  
  85. //var repeatedExam = userContestPoints
  86. // .ToDictionary(x => x.Key, x => x.ToDictionary(y => y.Key, y => y.Value)).Select(y=>y.Key );
  87. }
  88.  
  89. var bestUser = namePoints.OrderByDescending(x => x.Value).First().Key;
  90. var maxValue = namePoints.Max(x => x.Value);
  91.  
  92. Console.WriteLine($"Best candidate is {bestUser} with total {maxValue} points.");
  93. Console.WriteLine("Ranking:");
  94.  
  95. foreach (var kvp in userContestPoints.OrderBy(x => x.Key))
  96. {
  97. string user = kvp.Key;
  98. Dictionary<string, int> userContestResults = kvp.Value;
  99. Console.WriteLine(user);
  100.  
  101. foreach (var kvpValue in userContestResults.OrderByDescending(x => x.Value))
  102. {
  103. string contest = kvpValue.Key;
  104. int points = kvpValue.Value;
  105. Console.WriteLine($"# {contest} -> {points}");
  106. }
  107.  
  108. //userContestPoints.OrderBy(a => a.Key).SelectMany(a => a.Value.Select(b => "# " + b.Key + " -> " + b.Value))
  109. //.ToList()
  110. //.ForEach(Console.WriteLine);
  111. }
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement