Advertisement
Mike_Goodman92

Untitled

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