Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 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 int 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. if (inputLine.Length == 1 || inputLine.Length == 0)
  34. {
  35. inputLine = Console.ReadLine();
  36. continue;
  37. }
  38.  
  39. int getmeIndexAfterEventName = inputLine.IndexOf('@', 2);
  40.  
  41. if (getmeIndexAfterEventName == -1)
  42. {
  43. string getSubstring = inputLine.Substring(0);
  44. string[] giveMeParts = getSubstring.Split(' ');
  45.  
  46. EventObjectInfo emptyEvent = new EventObjectInfo();
  47. emptyEvent.Id = int.Parse(giveMeParts[0]);
  48. string trimStringOfAnnoyingHashtag = giveMeParts[1].Substring(1);
  49.  
  50.  
  51. //string trimStringOfAnnoyingHashtag = "";
  52.  
  53. //if (giveMeParts[1] != null)
  54. //{
  55.  
  56. //}
  57.  
  58.  
  59. if (!eventObjectInfoList.Any(x => x.EventName == trimStringOfAnnoyingHashtag))
  60. {
  61. emptyEvent.EventName = trimStringOfAnnoyingHashtag;
  62. eventObjectInfoList.Add(emptyEvent);
  63. }
  64.  
  65. inputLine = Console.ReadLine();
  66. continue;
  67. }
  68.  
  69. string getEventInforAndId = inputLine.Remove(getmeIndexAfterEventName); // останалото (@{participant1} @{participant2} … @{participantN}) от inputLine го махаме
  70. //да получим само {id} #{eventName} ->(това ни е остатъка)
  71.  
  72. bool isitValidEvent = ValidInputCheck(getEventInforAndId); // проверяваме дали {id} #{eventName} е валидно
  73.  
  74. string[] getFromgetEventInforAndIdTokens; // тука трябва да се съдържа //{id} #{eventName}
  75.  
  76. if (isitValidEvent) // ако true // то нека се вземат по отделно {id} #{eventName}
  77. {
  78. getFromgetEventInforAndIdTokens = getEventInforAndId.Split(new char[] { ' ', '#' }, StringSplitOptions.RemoveEmptyEntries);
  79. }
  80. else // ако не // наново да се почне
  81. {
  82. inputLine = Console.ReadLine();
  83. continue;
  84. }
  85.  
  86. // ако горното се е случило // вече имаме {id} #{eventName}
  87.  
  88. // трябва да вземе останалото
  89. string getParticipantsFromInput = inputLine.Substring(getmeIndexAfterEventName);
  90. string[] GetParticipantsNames = getParticipantsFromInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  91. //получаваме имената:
  92. // @{participant1}
  93. // @{participant2}
  94. // …
  95. // @{participantN}
  96.  
  97.  
  98. // ако проверката на това дали и участниците са написани с валидно име стане нужно !!!
  99. // също така всяко име да е уникално (да нямам повторения като @alice @alice @alice)
  100.  
  101. List<Participant> validParticipantList = GiveMeValidParticipantList(GetParticipantsNames);
  102.  
  103. //------------------------------------------------------------------------------------------
  104. // работа с обекти и класове
  105.  
  106. int idToken = int.Parse(getFromgetEventInforAndIdTokens[0]);
  107. string eventNameToken = getFromgetEventInforAndIdTokens[1];
  108.  
  109. // non - existing id
  110.  
  111. // ако не съществува го създай
  112. if (!eventObjectInfoList.Any(x => x.Id == idToken))
  113. {
  114. EventObjectInfo eventInformationPackage = new EventObjectInfo();
  115. eventInformationPackage.Id = idToken;
  116. eventInformationPackage.EventName = eventNameToken;
  117.  
  118. eventObjectInfoList.Add(eventInformationPackage);
  119. }
  120.  
  121. // взимаме създаденото и го филтрираме по сегашно зададения idToken
  122.  
  123. if (eventObjectInfoList.Any(x => x.EventName == eventNameToken && x.Id == idToken))
  124. {
  125. EventObjectInfo existingInformationPackage =
  126. eventObjectInfoList.Where(x => x.Id == idToken).First();
  127.  
  128. // ако същесвуват нека се добавят
  129. foreach (var item in validParticipantList)
  130. {
  131. if (!existingInformationPackage.ParticipantList.Any(x => x.ParticipantName == item.ParticipantName))
  132. {
  133. existingInformationPackage.ParticipantList.Add(item);
  134. }
  135. }
  136. }
  137.  
  138. inputLine = Console.ReadLine();
  139. }
  140.  
  141. foreach (var item in eventObjectInfoList.OrderByDescending(x => x.ParticipantList.Count).ThenBy(x => x.EventName))
  142. {
  143. Console.WriteLine($"{item.EventName} - {item.ParticipantList.Count}");
  144.  
  145. foreach (var item1 in item.ParticipantList.OrderBy(x => x.ParticipantName))
  146. {
  147. Console.WriteLine(item1.ParticipantName);
  148. }
  149. }
  150.  
  151.  
  152. }
  153.  
  154.  
  155. private static List<Participant> GiveMeValidParticipantList(string[] GetParticipantsNames)
  156. {
  157. List<Participant> result = new List<Participant>();
  158. string[] getParticipantsName = GetParticipantsNames;
  159. List<Participant> validParticipantList = ConvertStringArrayToParticipantList(getParticipantsName);
  160.  
  161. foreach (var item in validParticipantList)
  162. {
  163. bool validPartisimanFormat = ValidInputCheckForParticipants(item.ParticipantName);
  164. if (validPartisimanFormat && (!result.Any(x => x.ParticipantName == item.ParticipantName)))
  165. {
  166. result.Add(item);
  167. }
  168. }
  169. return result;
  170. }
  171.  
  172. private static List<Participant> ConvertStringArrayToParticipantList(string[] getParticipantsNames)
  173. {
  174. List<Participant> validParticipantList = new List<Participant>();
  175.  
  176. foreach (var item in getParticipantsNames)
  177. {
  178. Participant participantItem = new Participant(); // scope misstake ако е вънка :(
  179. participantItem.ParticipantName = item;
  180. validParticipantList.Add(participantItem);
  181. }
  182.  
  183. return validParticipantList;
  184. }
  185.  
  186. private static bool ValidInputCheckForParticipants(string item)
  187. {
  188. string pattern = @"@([A-Za-z])\w+";
  189. Regex patterForIdAndEventName = new Regex(pattern);
  190.  
  191. bool isitValidEvent = patterForIdAndEventName.IsMatch(item);
  192.  
  193. return isitValidEvent;
  194. }
  195.  
  196. private static bool ValidInputCheck(string getEventInforAndId)
  197. {
  198. string pattern = @"[0-9] #([A-Za-z])\w+";
  199. Regex patterForIdAndEventName = new Regex(pattern);
  200.  
  201. bool isitValidEvent = patterForIdAndEventName.IsMatch(getEventInforAndId);
  202.  
  203. return isitValidEvent;
  204. }
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement