Advertisement
bullit3189

10. SoftUni Exam Results - Dictionaries

Jan 17th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Program
  6. {
  7. public static void Main()
  8. {
  9. Dictionary <string, int> studentsAndPoints = new Dictionary<string, int>();
  10. Dictionary <string, int> langAndCount = new Dictionary<string, int>();
  11.  
  12. while(true)
  13. {
  14. string line = Console.ReadLine();
  15.  
  16. if (line == "exam finished")
  17. {
  18. break;
  19. }
  20.  
  21. string[] tokens = line.Split('-');
  22.  
  23. if (tokens.Length>2)
  24. {
  25. string username = tokens[0];
  26. string language = tokens[1];
  27. int points = int.Parse(tokens[2]);
  28.  
  29. if (!langAndCount.ContainsKey(language))
  30. {
  31. langAndCount[language]=1;
  32. }
  33. else
  34. {
  35. langAndCount[language]++;
  36. }
  37.  
  38.  
  39.  
  40. if (!studentsAndPoints.ContainsKey(username))
  41. {
  42. studentsAndPoints[username]=points;
  43. }
  44. else
  45. {
  46. if (points>studentsAndPoints[username])
  47. {
  48. studentsAndPoints[username]=points;
  49. }
  50. }
  51. }
  52. else
  53. {
  54. string username = tokens[0];
  55. string banned = tokens[1];
  56.  
  57. if (studentsAndPoints.ContainsKey(username))
  58. {
  59. studentsAndPoints.Remove(username);
  60. }
  61. }
  62. }
  63.  
  64. Console.Write("Results:");
  65. Console.WriteLine();
  66.  
  67. foreach (var kvp in studentsAndPoints.OrderByDescending(x=>x.Value).ThenBy(x=>x.Key))
  68. {
  69. Console.WriteLine("{0} | {1}",kvp.Key,kvp.Value);
  70. }
  71.  
  72. Console.WriteLine("Submissions:");
  73.  
  74. foreach (var kvp in langAndCount.OrderByDescending(x=>x.Value).ThenBy(x=>x.Key))
  75. {
  76. Console.WriteLine("{0} - {1}",kvp.Key,kvp.Value);
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement