Advertisement
Guest User

Main

a guest
Jun 9th, 2016
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. namespace StudentGroups
  7. {
  8. public class StudentGroups
  9. {
  10. public static void Main()
  11. {
  12. List<Town> result = ReadTownsAndStudents();
  13. List<Group> finish = DistributeStudentsInGroups(result);
  14.  
  15. Console.WriteLine("Created {0} groups in {1} towns:", finish.Count, result.Count);
  16. }
  17.  
  18. public static List<Town> ReadTownsAndStudents()
  19. {
  20. List<Town> towns = new List<Town>();
  21.  
  22. string inputLine = Console.ReadLine();
  23. while (inputLine != "End")
  24. {
  25. if (inputLine.Contains("=>"))
  26. {
  27. string[] parsedTownInput = inputLine.Split(new char[] { '=', '>' }, StringSplitOptions.RemoveEmptyEntries);
  28. string[] seats = parsedTownInput[1].Trim().Split();
  29.  
  30. Town town = new Town();
  31. List<Student> students = new List<Student>();
  32.  
  33. string townName = parsedTownInput[0].Trim();
  34. int seatCount = int.Parse(seats[0].Trim());
  35.  
  36. town.Name = townName;
  37. town.SeatCount = seatCount;
  38. town.Students = students;
  39. towns.Add(town);
  40. }
  41. else
  42. {
  43. Student student = new Student();
  44. Town town = new Town();
  45. town.Students = new List<Student>();
  46. string[] parsedStudentInput = inputLine.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  47.  
  48. string studentName = parsedStudentInput[0].Trim();
  49. string studentEmail = parsedStudentInput[1].Trim();
  50. DateTime studentRegistrationDateTime = DateTime.ParseExact(parsedStudentInput[2].Trim(), "d-MMM-yyyy",
  51. CultureInfo.InvariantCulture);
  52.  
  53. student.Name = studentName;
  54. student.Email = studentEmail;
  55. student.RegistrationDate = studentRegistrationDateTime;
  56.  
  57. town.Students.Add(student);
  58. }
  59. inputLine = Console.ReadLine();
  60. }
  61. return towns;
  62. }
  63.  
  64. static List<Group> DistributeStudentsInGroups(List<Town> towns)
  65. {
  66. var town = new Town();
  67. town.Students = new List<Student>();
  68. var groups = new List<Group>();
  69.  
  70. for (int i = 0; i < towns.Count; i++)
  71. {
  72. List<Student> students = towns[i].Students;
  73.  
  74. students = towns[i].Students.OrderBy(x => x.RegistrationDate)
  75. .ThenBy(x => x.Name.Distinct())
  76. .ThenBy(x => x.Email)
  77. .ToList();
  78.  
  79. while (students.Any())
  80. {
  81. Group group = new Group();
  82.  
  83. group.Town = towns[i];
  84. group.Students = students.Take(group.Town.SeatCount).ToList();
  85. students = students.Skip(group.Town.SeatCount).ToList();
  86. groups.Add(group);
  87. }
  88. }
  89. return groups;
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement