Advertisement
bullit3189

Courses - Dictionaries

Jan 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 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,List<string>> coursesAndStudents = new Dictionary<string,List<string>>();
  10.  
  11. while(true)
  12. {
  13. string command = Console.ReadLine();
  14.  
  15. if (command == "end")
  16. {
  17. break;
  18. }
  19.  
  20. string[] tokens = command.Split(':').Select(s => s.Trim()).ToArray();
  21.  
  22. string courseName = tokens[0];
  23. string studentName = tokens[1];
  24.  
  25. if (!coursesAndStudents.ContainsKey(courseName))
  26. {
  27. coursesAndStudents.Add(courseName, new List<string>());
  28. coursesAndStudents[courseName].Add(studentName);
  29. }
  30. else
  31. {
  32. coursesAndStudents[courseName].Add(studentName);
  33. }
  34. }
  35.  
  36. foreach (var kvp in coursesAndStudents.OrderByDescending(x=>x.Value.Count))
  37. {
  38. Console.WriteLine("{0}: {1}",kvp.Key,kvp.Value.Count());
  39.  
  40. foreach (var name in kvp.Value.OrderBy(n=>n))
  41. {
  42. Console.WriteLine("-- {0}",name);
  43. }
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement