Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Town> towns = new List<Town>();
- string input = Console.ReadLine();
- towns = GetDataRecursively(input, towns);
- towns = SortedTowns(towns);
- towns = GroupingStudents(towns);
- Print(towns);
- }
- public static Town CreateTown(string input)
- {
- Town town = new Town();
- string[] townInfo = input.Split(new[] { " => " }, StringSplitOptions.RemoveEmptyEntries);
- town.Name = townInfo[0];
- town.SeatCapacity = townInfo[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).First();
- town.Students = new List<Student>();
- town.Groups = new List<Group>();
- return town;
- }
- public static Student CreateStudent(string input)
- {
- string[] studentInfo = Regex.Split(input, @"\s*\|\s*");
- Student student = new Student();
- student.Name = studentInfo[0];
- student.email = studentInfo[1];
- student.RegistrationDate = DateTime.ParseExact(studentInfo[2], "d-MMM-yyyy", CultureInfo.InvariantCulture);
- return student;
- }
- public static void Print(List<Town> towns)
- {
- Console.WriteLine($"Created {towns.Sum(x => x.Groups.Count)} groups in {towns.Count} towns:");
- foreach (var town in towns.OrderBy(x => x.Name))
- {
- foreach (var group in town.Groups)
- {
- List<string> emails = new List<string>();
- foreach (var student in group.StudentsInGroup)
- {
- emails.Add(student.email);
- }
- Console.WriteLine($"{town.Name} => { string.Join(", ", emails)}");
- }
- }
- return;
- }
- public static List<Town> GetDataRecursively(string input, List<Town> towns)
- {
- if (input == "End")
- {
- return towns;
- }
- Town town = CreateTown(input);
- while (true)
- {
- input = Console.ReadLine();
- if (!input.Contains(" => ") && input != "End")
- {
- Student student = CreateStudent(input);
- town.Students.Add(student);
- }
- else
- {
- towns.Add(town);
- if (input == "End")
- {
- break;
- }
- else
- {
- towns = GetDataRecursively(input, towns);
- break;
- }
- }
- }
- return towns;
- }
- public static List<Town> SortedTowns(List<Town> towns)
- {
- for (int i = 0; i < towns.Count; i++)
- {
- towns[i].Students = towns[i].Students.OrderBy(z => z.RegistrationDate).ThenBy(y => y.Name).ThenBy(x => x.email).ToList();
- }
- return towns;
- }
- public static List<Town> GroupingStudents(List<Town> towns)
- {
- foreach (var town in towns)
- {
- Group group = new Group();
- group.StudentsInGroup = new List<Student>();
- int studentCounter = 0;
- foreach (var student in town.Students)
- {
- studentCounter++;
- if (group.StudentsInGroup.Count < town.SeatCapacity)
- {
- group.StudentsInGroup.Add(student);
- if (studentCounter == town.Students.Count)
- {
- town.Groups.Add(group);
- }
- }
- else
- {
- town.Groups.Add(group);
- group = new Group();
- group.StudentsInGroup = new List<Student>();
- group.StudentsInGroup.Add(student);
- if (studentCounter == town.Students.Count)
- {
- town.Groups.Add(group);
- }
- }
- }
- }
- return towns;
- }
- }
- class Town
- {
- public List<Student> Students { get; set; }
- public string Name { get; set; }
- public int SeatCapacity { get; set; }
- public List<Group> Groups { get; set; }
- }
- class Student
- {
- public string Name { get; set; }
- public string email { get; set; }
- public DateTime RegistrationDate { get; set; }
- }
- class Group
- {
- public List<Student> StudentsInGroup { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement