public Group(Town town) { this.Town = town; this.Students = new List <Student>(); }
static void Main() { List <Group> groups = new List <Group>(); List <Town> towns = new List <Town>(); string input = Console.ReadLine(); int counter = -1; while (input != "End") { if (input.Contains("=>")) { string[] inputs = input.Split(new char[] { '=', '>' }, StringSplitOptions.RemoveEmptyEntries); string townName = inputs[0].Trim(); int seats = int.Parse(inputs[1].Trim().Split(' ')[0]); var town = new Town() { Name = townName, SeatsCount = seats, Students = new List <Student>() }; towns.Add(town); counter++; } else { string[] namesInput = input.Split('|'); string name = namesInput[0].Trim(); string email = namesInput[1].Trim(); DateTime date = DateTime.ParseExact(namesInput[2].Trim(), "d-MMM-yyyy", CultureInfo.InvariantCulture); var student = new Student() { Name = name, Email = email, RegistrationDate = date }; towns[counter].Students.Add(student); } input = Console.ReadLine(); } foreach (var town in towns) { town.Students = town.Students.OrderBy(x => x.RegistrationDate).ThenBy(x => x.Name).ThenBy(x => x.Email).ToList(); int countProcessedStudents = 0; while (countProcessedStudents < town.Students.Count) { var group = new Group() { Town = town, Students = new List <Student>() }; groups.Add(group); for (int i = countProcessedStudents; i < town.Students.Count; i++) { group.Students.Add(town.Students[i]); countProcessedStudents++; if (group.Students.Count == town.SeatsCount) { break; } } } } Console.WriteLine("Created {0} groups in {1} towns:", groups.Count, towns.Count); foreach (var group in groups.OrderBy(x => x.Town.Name)) { Console.WriteLine("{0} => {1}", group.Town.Name, String.Join(", ", group.Students.Select(x => x.Email))); } }
static void Main(string[] args) { string input = Console.ReadLine(); List <Town> towns = new List <Town>(); Town currentTown = new Town(); while (input != "End") { if (input.Contains("=>")) { string[] townInfo = input.Split(new char[] { '=', '>' }, StringSplitOptions.RemoveEmptyEntries); string town = townInfo[0].Trim(); string lab = townInfo[1].Trim(); int labCapacity = int.Parse(lab[0].ToString()); Town newTown = new Town(); newTown.Name = town; newTown.LabCapacity = labCapacity; newTown.Students = new List <Student>(); currentTown = newTown; towns.Add(newTown); } else { string[] studentInfo = input.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); string name = studentInfo[0].Trim(); string email = studentInfo[1].Trim(); string date = studentInfo[2].Trim(); DateTime regDate = DateTime.ParseExact(date, "d-MMM-yyyy", CultureInfo.InvariantCulture); Student newStudent = new Student(); newStudent.Name = name; newStudent.Email = email; newStudent.RegistrationDate = regDate; currentTown.Students.Add(newStudent); } input = Console.ReadLine(); } List <Group> groups = new List <Group>(); foreach (var town in towns) { IEnumerable <Student> townStudents = town.Students .OrderBy(x => x.RegistrationDate).ThenBy(x => x.Name).ThenBy(x => x.Email); while (townStudents.Any()) { Group newGroup = new Group(); newGroup.Town = town; newGroup.Students = townStudents.Take(newGroup.Town.LabCapacity).ToList(); townStudents = townStudents.Skip(newGroup.Town.LabCapacity); groups.Add(newGroup); } } Console.WriteLine($"Created {groups.Count} groups in {towns.Select(x => x.Name).Distinct().ToList().Count} towns:"); foreach (var group in groups.OrderBy(x => x.Town.Name)) { Console.Write("{0} => ", group.Town.Name); string emails = ""; foreach (var student in group.Students) { emails += student.Email + ", "; } emails = emails.TrimEnd(new char[] { ' ', ',' }); Console.WriteLine(emails); } }