Advertisement
fbinnzhivko

Untitled

Jan 17th, 2017
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. class StudentGroups
  10. {
  11.     static void Main()
  12.     {
  13.         var tokens = Group.ReadTownsAndStudents();
  14.  
  15.         var result = Group.DistributeStudentsIntoGroups(tokens);
  16.         Console.WriteLine($"Created {result.Count} groups in {result.Select(a=>a.Town).Distinct().ToList().Count} towns:");
  17.  
  18.         foreach (var group in result)
  19.         {
  20.             Console.WriteLine($"{group.Town.Name} => {string.Join(", ", group.Students.Select(a=>a.Email).ToList())}");
  21.         }
  22.  
  23.     }
  24. }
  25.  
  26. public class Town
  27. {
  28.     public string Name { get; set; }
  29.  
  30.     public int SeatsCount { get; set; }
  31.  
  32.     public List<Student> Students { get; set; }
  33. }
  34.  
  35. public class Student
  36. {
  37.     public string Name { get; set; }
  38.  
  39.     public string Email { get; set; }
  40.  
  41.     public DateTime RegistrationDate { get; set; }
  42. }
  43.  
  44. class Group
  45. {
  46.     public Town Town { get; set; }
  47.     public List<Student> Students { get; set; }
  48.  
  49.     public static List<Town> ReadTownsAndStudents()
  50.     {
  51.         var towns = new List<Town>();
  52.  
  53.         string inputLine = Console.ReadLine();
  54.  
  55.         while (inputLine != "End")
  56.         {
  57.             if (inputLine.IndexOf("=>") != -1)
  58.             {
  59.                 Town town = new Town();
  60.                 string[] parseInput = inputLine.Trim().Split(new[] { " => " }, StringSplitOptions.RemoveEmptyEntries);
  61.  
  62.                 town.Name = parseInput[0];
  63.                 town.SeatsCount = int.Parse(parseInput[1].Split(' ')[0]);
  64.                 town.Students = new List<Student>();
  65.                 towns.Add(town);
  66.             }
  67.             else
  68.             {
  69.                 Student student = new Student();
  70.                 string[] parseInput = inputLine.Split(new[] {'|', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  71.  
  72.                 student.Name = parseInput[0] + " " + parseInput[1];
  73.                 student.Email = parseInput[2];
  74.                 if (parseInput[2].Contains("|"))
  75.                 {
  76.                     student.Email = student.Email.Substring(1, parseInput[2].Length - 1);
  77.                 }
  78.                 if (parseInput[3].Contains("|"))
  79.                 {
  80.                     parseInput[3] = parseInput[3].Substring(1, parseInput[3].Length - 1);
  81.                 }
  82.  
  83.                 student.RegistrationDate = DateTime.ParseExact(parseInput[3], "d-MMM-yyyy", CultureInfo.InvariantCulture);
  84.                 towns[towns.Count-1].Students.Add(student);
  85.             }
  86.  
  87.             inputLine = Console.ReadLine();
  88.         }
  89.  
  90.         return towns;
  91.     }
  92.  
  93.     public static List<Group> DistributeStudentsIntoGroups(List<Town> towns)
  94.     {
  95.         var groups = new List<Group>();
  96.  
  97.         foreach (var town in towns.OrderBy(x=>x.Name))
  98.         {
  99.             IEnumerable<Student> students =
  100.                 town.Students.OrderBy(x => x.RegistrationDate).ThenBy(n => n.Name).ThenBy(e => e.Email);
  101.  
  102.             while (students.Any())
  103.             {
  104.                 var group = new Group();
  105.                 group.Town = town;
  106.                 group.Students = students.Take(group.Town.SeatsCount).ToList();
  107.                 students = students.Skip(group.Town.SeatsCount);
  108.                 groups.Add(group);
  109.             }
  110.         }
  111.         return groups;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement