Advertisement
KKK99

06.Courses

Nov 15th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Courses
  8. {
  9.     public class Course
  10.     {
  11.         public string Courses { get; set; }
  12.         public int People { get; set; }
  13.         public List<string> Students { get; set; }
  14.  
  15.         public Course(string courses)
  16.         {
  17.             this.Courses = courses;
  18.             this.People = 1;
  19.             this.Students = new List<string>();
  20.  
  21.         }
  22.     }
  23.     class Program
  24.     {
  25.         static void Main(string[] args)
  26.         {
  27.             List<Course> CoursesAndStudents = new List<Course>();
  28.             while (true)
  29.             {
  30.  
  31.                 List<string> information = Console.ReadLine()
  32.                     .Split(new[] { " : " }, StringSplitOptions.RemoveEmptyEntries)
  33.                     .ToList();
  34.                 string courses = information[0];
  35.  
  36.                 if (courses == "end")
  37.                 {
  38.                     break;
  39.                 }
  40.                 string person = information[1];
  41.                 bool isExist = CoursesAndStudents
  42.                     .Any(x => x.Courses.Contains(courses));
  43.                 Course addingStudents = new Course(courses);
  44.  
  45.                 if (!isExist)
  46.                 {
  47.                     CoursesAndStudents.Add(addingStudents);
  48.                     int index = CoursesAndStudents.FindIndex(x => x.Courses == courses);
  49.                     CoursesAndStudents[index].Students.Add(person);
  50.  
  51.                 }
  52.                 else
  53.                 {
  54.                     int index = CoursesAndStudents.FindIndex(x => x.Courses == courses);
  55.                     CoursesAndStudents[index].Students.Add(person);
  56.                     CoursesAndStudents[index].People++;
  57.                 }
  58.  
  59.  
  60.             }
  61.             var SortedCoursesAndStudents = CoursesAndStudents
  62.                .OrderByDescending(x => x.People);
  63.  
  64.  
  65.  
  66.  
  67.             foreach (var kvp in SortedCoursesAndStudents)
  68.             {
  69.                 Console.WriteLine($"{kvp.Courses}: {kvp.People}");
  70.  
  71.                 foreach (var student in kvp.Students.OrderBy(x => x))
  72.                 {
  73.                     Console.WriteLine($"-- {student}");
  74.                 }
  75.             }
  76.         }
  77.  
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement