JulianJulianov

11.AsociativeArrays-Courses

Apr 12th, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. 11. Courses
  2. Write a program that keeps information about courses. Each course has a name and registered students.
  3. You will be receiving a course name and a student name, until you receive the command "end". Check if such course already exists, and if not, add the course. Register the user into the course. When you receive the command "end", print the courses with their names and total registered users, ordered by the count of registered users in descending order. For each contest print the registered users ordered by name in ascending order.
  4. Input
  5. • Until the "end" command is received, you will be receiving input in the format: "{courseName} : {studentName}".
  6. • The product data is always delimited by " : ".
  7. Output
  8. • Print the information about each course in the following the format:
  9. "{courseName}: {registeredStudents}"
  10. • Print the information about each student, in the following the format:
  11. "-- {studentName}"
  12. Examples
  13. Input                                                  Output
  14. Programming Fundamentals : John Smith                  Programming Fundamentals: 2
  15. Programming Fundamentals : Linda Johnson               -- John Smith
  16. JS Core : Will Wilson                                  -- Linda Johnson
  17. Java Advanced : Harrison White                         JS Core: 1
  18. end                                                    -- Will Wilson
  19.                                                        Java Advanced: 1
  20.                                                        -- Harrison White
  21.  
  22.  
  23.  
  24. Algorithms : Jay Moore                                 Python Fundamentals: 3
  25. Programming Basics : Martin Taylor                     -- Andrew Robinson
  26. Python Fundamentals : John Anderson                    -- Clark Lewis
  27. Python Fundamentals : Andrew Robinson                  -- John Anderson
  28. Algorithms : Bob Jackson                               Algorithms: 2
  29. Python Fundamentals : Clark Lewis                      -- Bob Jackson
  30. end                                                    -- Jay Moore
  31.                                                        Programming Basics: 1
  32.                                                        -- Martin Taylor
  33.  
  34.  
  35.  
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39.  
  40. namespace _11Courses
  41. {
  42.     class Program
  43.     {
  44.         static void Main(string[] args)
  45.         {
  46.             var registered = new Dictionary<string, List<string>>();
  47.  
  48.             string courseAndStudent;
  49.  
  50.             while ((courseAndStudent = Console.ReadLine()) != "end")
  51.             {
  52.                 var courseSplitted = courseAndStudent.Split(" : ");
  53.  
  54.                 var courseName = courseSplitted[0];
  55.                 var studentName = courseSplitted[1];
  56.  
  57.                 if (!registered.ContainsKey(courseName))
  58.                 {
  59.                     registered.Add(courseName, new List<string>() { studentName });
  60.                 }
  61.                 else
  62.                 {
  63.                     registered[courseName].Add(studentName);
  64.                 }
  65.             }
  66.  
  67.             foreach (var course in registered.OrderByDescending(x => x.Value.Count))
  68.             {
  69.                 Console.WriteLine($"{course.Key}: {course.Value.Count()}");
  70.  
  71.                 foreach (var name in course.Value.OrderBy(x => x))
  72.                 {
  73.                     Console.WriteLine($"-- {name}");
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment