Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace Problem_4.Roli___The_Coder
- {
- class Program
- {
- class EventObjectInfo
- {
- public int Id { get; set; }
- public string EventName { get; set; }
- public List<Participant> ParticipantList = new List<Participant>();
- }
- class Participant
- {
- public string ParticipantName { get; set; }
- }
- static void Main(string[] args)
- {
- List<EventObjectInfo> eventObjectInfoList = new List<EventObjectInfo>();
- string inputLine = Console.ReadLine();
- // {id} #{eventName} @{participant1} @{participant2} … @{participantN}
- while (inputLine != "Time for Code")
- {
- if (inputLine.Length == 1 || inputLine.Length == 0)
- {
- inputLine = Console.ReadLine();
- continue;
- }
- int getmeIndexAfterEventName = inputLine.IndexOf('@', 2);
- if (getmeIndexAfterEventName == -1)
- {
- string getSubstring = inputLine.Substring(0);
- string[] giveMeParts = getSubstring.Split(' ');
- EventObjectInfo emptyEvent = new EventObjectInfo();
- emptyEvent.Id = int.Parse(giveMeParts[0]);
- string trimStringOfAnnoyingHashtag = giveMeParts[1].Substring(1);
- //string trimStringOfAnnoyingHashtag = "";
- //if (giveMeParts[1] != null)
- //{
- //}
- if (!eventObjectInfoList.Any(x => x.EventName == trimStringOfAnnoyingHashtag))
- {
- emptyEvent.EventName = trimStringOfAnnoyingHashtag;
- eventObjectInfoList.Add(emptyEvent);
- }
- inputLine = Console.ReadLine();
- continue;
- }
- string getEventInforAndId = inputLine.Remove(getmeIndexAfterEventName); // останалото (@{participant1} @{participant2} … @{participantN}) от inputLine го махаме
- //да получим само {id} #{eventName} ->(това ни е остатъка)
- bool isitValidEvent = ValidInputCheck(getEventInforAndId); // проверяваме дали {id} #{eventName} е валидно
- string[] getFromgetEventInforAndIdTokens; // тука трябва да се съдържа //{id} #{eventName}
- if (isitValidEvent) // ако true // то нека се вземат по отделно {id} #{eventName}
- {
- getFromgetEventInforAndIdTokens = getEventInforAndId.Split(new char[] { ' ', '#' }, StringSplitOptions.RemoveEmptyEntries);
- }
- else // ако не // наново да се почне
- {
- inputLine = Console.ReadLine();
- continue;
- }
- // ако горното се е случило // вече имаме {id} #{eventName}
- // трябва да вземе останалото
- string getParticipantsFromInput = inputLine.Substring(getmeIndexAfterEventName);
- string[] GetParticipantsNames = getParticipantsFromInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- //получаваме имената:
- // @{participant1}
- // @{participant2}
- // …
- // @{participantN}
- // ако проверката на това дали и участниците са написани с валидно име стане нужно !!!
- // също така всяко име да е уникално (да нямам повторения като @alice @alice @alice)
- List<Participant> validParticipantList = GiveMeValidParticipantList(GetParticipantsNames);
- //------------------------------------------------------------------------------------------
- // работа с обекти и класове
- int idToken = int.Parse(getFromgetEventInforAndIdTokens[0]);
- string eventNameToken = getFromgetEventInforAndIdTokens[1];
- // non - existing id
- // ако не съществува го създай
- if (!eventObjectInfoList.Any(x => x.Id == idToken))
- {
- EventObjectInfo eventInformationPackage = new EventObjectInfo();
- eventInformationPackage.Id = idToken;
- eventInformationPackage.EventName = eventNameToken;
- eventObjectInfoList.Add(eventInformationPackage);
- }
- // взимаме създаденото и го филтрираме по сегашно зададения idToken
- if (eventObjectInfoList.Any(x => x.EventName == eventNameToken && x.Id == idToken))
- {
- EventObjectInfo existingInformationPackage =
- eventObjectInfoList.Where(x => x.Id == idToken).First();
- // ако същесвуват нека се добавят
- foreach (var item in validParticipantList)
- {
- if (!existingInformationPackage.ParticipantList.Any(x => x.ParticipantName == item.ParticipantName))
- {
- existingInformationPackage.ParticipantList.Add(item);
- }
- }
- }
- inputLine = Console.ReadLine();
- }
- foreach (var item in eventObjectInfoList.OrderByDescending(x => x.ParticipantList.Count).ThenBy(x => x.EventName))
- {
- Console.WriteLine($"{item.EventName} - {item.ParticipantList.Count}");
- foreach (var item1 in item.ParticipantList.OrderBy(x => x.ParticipantName))
- {
- Console.WriteLine(item1.ParticipantName);
- }
- }
- }
- private static List<Participant> GiveMeValidParticipantList(string[] GetParticipantsNames)
- {
- List<Participant> result = new List<Participant>();
- string[] getParticipantsName = GetParticipantsNames;
- List<Participant> validParticipantList = ConvertStringArrayToParticipantList(getParticipantsName);
- foreach (var item in validParticipantList)
- {
- bool validPartisimanFormat = ValidInputCheckForParticipants(item.ParticipantName);
- if (validPartisimanFormat && (!result.Any(x => x.ParticipantName == item.ParticipantName)))
- {
- result.Add(item);
- }
- }
- return result;
- }
- private static List<Participant> ConvertStringArrayToParticipantList(string[] getParticipantsNames)
- {
- List<Participant> validParticipantList = new List<Participant>();
- foreach (var item in getParticipantsNames)
- {
- Participant participantItem = new Participant(); // scope misstake ако е вънка :(
- participantItem.ParticipantName = item;
- validParticipantList.Add(participantItem);
- }
- return validParticipantList;
- }
- private static bool ValidInputCheckForParticipants(string item)
- {
- string pattern = @"@([A-Za-z])\w+";
- Regex patterForIdAndEventName = new Regex(pattern);
- bool isitValidEvent = patterForIdAndEventName.IsMatch(item);
- return isitValidEvent;
- }
- private static bool ValidInputCheck(string getEventInforAndId)
- {
- string pattern = @"[0-9] #([A-Za-z])\w+";
- Regex patterForIdAndEventName = new Regex(pattern);
- bool isitValidEvent = patterForIdAndEventName.IsMatch(getEventInforAndId);
- return isitValidEvent;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement