Mike_Goodman92

Untitled

Oct 22nd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Problem_4.Roli___The_Coder
  9. {
  10. class Program
  11. {
  12. class EventObjectInfo
  13. {
  14. public long Id { get; set; }
  15. public string EventName { get; set; }
  16. public List<Participant> ParticipantList = new List<Participant>();
  17. }
  18.  
  19. class Participant
  20. {
  21. public string ParticipantName { get; set; }
  22. }
  23.  
  24. static void Main(string[] args)
  25. {
  26. List<EventObjectInfo> eventObjectInfoList = new List<EventObjectInfo>();
  27.  
  28. string inputLine = Console.ReadLine();
  29. // {id} #{eventName} @{participant1} @{participant2} … @{participantN}
  30.  
  31. while (inputLine != "Time for Code")
  32. {
  33. string pattern = @"[0-9]+ #[A-Za-z]+";
  34. Regex checkIfpatternIs = new Regex(pattern);
  35. bool isValid = checkIfpatternIs.IsMatch(inputLine);
  36.  
  37. if (!isValid)
  38. {
  39. inputLine = Console.ReadLine();
  40. continue;
  41. }
  42.  
  43. int getmeIndexAfterEventName = inputLine.IndexOf('@', 2);
  44.  
  45. if (getmeIndexAfterEventName == -1)
  46. {
  47. string getSubstring = inputLine.Substring(0);
  48. string[] giveMeParts = getSubstring.Split(' ');
  49.  
  50. EventObjectInfo emptyEvent = new EventObjectInfo();
  51. emptyEvent.Id = long.Parse(giveMeParts[0]);
  52.  
  53. string trimStringOfAnnoyingHashtag = giveMeParts[1].Substring(1);
  54.  
  55. if (!eventObjectInfoList.Any(x => x.EventName == trimStringOfAnnoyingHashtag))
  56. {
  57. emptyEvent.EventName = trimStringOfAnnoyingHashtag;
  58. eventObjectInfoList.Add(emptyEvent);
  59. }
  60.  
  61. inputLine = Console.ReadLine();
  62. continue;
  63. }
  64.  
  65. string getEventInforAndId = inputLine.Remove(getmeIndexAfterEventName); // останалото (@{participant1} @{participant2} … @{participantN}) от inputLine го махаме
  66. //да получим само {id} #{eventName} ->(това ни е остатъка)
  67.  
  68. string[] getFromgetEventInforAndIdTokens = getEventInforAndId.Split(new char[] { ' ', '#' }, StringSplitOptions.RemoveEmptyEntries); ; // тука трябва да се съдържа //{id} #{eventName}
  69.  
  70. // трябва да вземе останалото
  71. string getParticipantsFromInput = inputLine.Substring(getmeIndexAfterEventName);
  72. string[] GetParticipantsNames = getParticipantsFromInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  73. //получаваме имената:
  74. // @{participant1}
  75. // @{participant2}
  76. // …
  77. // @{participantN}
  78.  
  79.  
  80. // ако проверката на това дали и участниците са написани с валидно име стане нужно !!!
  81. // също така всяко име да е уникално (да нямам повторения като @alice @alice @alice)
  82.  
  83. List<Participant> validParticipantList = GiveMeValidParticipantList(GetParticipantsNames);
  84.  
  85. //------------------------------------------------------------------------------------------
  86. // работа с обекти и класове
  87.  
  88. long idToken = long.Parse(getFromgetEventInforAndIdTokens[0]);
  89. string eventNameToken = getFromgetEventInforAndIdTokens[1];
  90.  
  91. // non - existing id
  92.  
  93. // ако не съществува го създай
  94. if (!eventObjectInfoList.Any(x => x.Id == idToken))
  95. {
  96. EventObjectInfo eventInformationPackage = new EventObjectInfo();
  97. eventInformationPackage.Id = idToken;
  98. eventInformationPackage.EventName = eventNameToken;
  99.  
  100. eventObjectInfoList.Add(eventInformationPackage);
  101. }
  102.  
  103. // взимаме създаденото и го филтрираме по сегашно зададения idToken
  104.  
  105. if (eventObjectInfoList.Any(x => x.EventName == eventNameToken && x.Id == idToken))
  106. {
  107. EventObjectInfo existingInformationPackage =
  108. eventObjectInfoList.Where(x => x.Id == idToken).First();
  109.  
  110. // ако същесвуват нека се добавят
  111. foreach (var item in validParticipantList)
  112. {
  113. if (!existingInformationPackage.ParticipantList.Any(x => x.ParticipantName == item.ParticipantName))
  114. {
  115. existingInformationPackage.ParticipantList.Add(item);
  116. }
  117. }
  118. }
  119.  
  120. inputLine = Console.ReadLine();
  121. }
  122.  
  123. // Output :
  124.  
  125. foreach (var item in eventObjectInfoList.OrderByDescending(x => x.ParticipantList.Count).ThenBy(x => x.EventName))
  126. {
  127. Console.WriteLine($"{item.EventName} - {item.ParticipantList.Count}");
  128.  
  129. foreach (var item1 in item.ParticipantList.OrderBy(x => x.ParticipantName))
  130. {
  131. Console.WriteLine(item1.ParticipantName);
  132. }
  133. }
  134.  
  135.  
  136. }
  137.  
  138.  
  139. private static List<Participant> GiveMeValidParticipantList(string[] GetParticipantsNames)
  140. {
  141. List<Participant> result = new List<Participant>();
  142. string[] getParticipantsName = GetParticipantsNames;
  143. List<Participant> validParticipantList = ConvertStringArrayToParticipantList(getParticipantsName);
  144.  
  145. foreach (var item in validParticipantList)
  146. {
  147. bool validPartisimanFormat = ValidInputCheckForParticipants(item.ParticipantName);
  148. if (validPartisimanFormat && (!result.Any(x => x.ParticipantName == item.ParticipantName)))
  149. {
  150. result.Add(item);
  151. }
  152. }
  153. return result;
  154. }
  155.  
  156. private static List<Participant> ConvertStringArrayToParticipantList(string[] getParticipantsNames)
  157. {
  158. List<Participant> validParticipantList = new List<Participant>();
  159.  
  160. foreach (var item in getParticipantsNames)
  161. {
  162. Participant participantItem = new Participant(); // scope misstake ако е вънка :(
  163. participantItem.ParticipantName = item;
  164. validParticipantList.Add(participantItem);
  165. }
  166.  
  167. return validParticipantList;
  168. }
  169.  
  170. private static bool ValidInputCheckForParticipants(string item)
  171. {
  172. string pattern = @"@([A-Za-z])\w+";
  173. Regex patterForIdAndEventName = new Regex(pattern);
  174.  
  175. bool isitValidEvent = patterForIdAndEventName.IsMatch(item);
  176.  
  177. return isitValidEvent;
  178. }
  179.  
  180. private static bool ValidInputCheck(string getEventInforAndId)
  181. {
  182. string pattern = @"[0-9] #([A-Za-z])\w+";
  183. Regex patterForIdAndEventName = new Regex(pattern);
  184.  
  185. bool isitValidEvent = patterForIdAndEventName.IsMatch(getEventInforAndId);
  186.  
  187. return isitValidEvent;
  188. }
  189. }
  190. }
Add Comment
Please, Sign In to add comment