Guest User

06-Courses-Tech

a guest
Oct 31st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. namespace associativeArrays
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             string until = string.Empty;
  12.  
  13.             var dict = new Dictionary<string, List<string>>();
  14.  
  15.             while ((until = Console.ReadLine()) != "end")
  16.             {
  17.                 string[] tokens = until.Split(new[] { " : " }, StringSplitOptions.RemoveEmptyEntries);
  18.                 string nameOfCourse = tokens[0];
  19.                 string student = tokens[1];
  20.  
  21.                 if (dict.ContainsKey(nameOfCourse) == false)
  22.                 {
  23.                     dict.Add(nameOfCourse, new List<string>());
  24.                     dict[nameOfCourse].Add(student);
  25.                 }
  26.  
  27.                 // Проверка дали един и същ курсист е записан в повече от един път в курса.
  28.                 else if (dict[nameOfCourse].Contains(student) == false)
  29.                 {
  30.                     dict[nameOfCourse].Add(student);
  31.                 }
  32.             }
  33.  
  34.             // Подредба по бройка на студенти в низходящ ред както е по условие.
  35.             var ordered = dict.OrderByDescending(x => x.Value.Count);
  36.  
  37.             foreach (var kvp in ordered)
  38.             {
  39.                 string program = kvp.Key;
  40.  
  41.                 // Подредба по азбучен ред.
  42.                 var students = kvp.Value.OrderBy(x => x).ToList();
  43.  
  44.                 Console.WriteLine($"{program}: {kvp.Value.Count}");
  45.                 //Console.Write($"-- ");
  46.                 //Console.WriteLine(string.Join($"{Environment.NewLine}-- ", students));
  47.  
  48.                 // Това е по-чисто принтиране, но и горното работи.
  49.                 students.ForEach(x => { Console.WriteLine($"-- {x}"); });
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment