Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         Dictionary<int, Dictionary<string, List<string>>> eventDict =
  10.             new Dictionary<int, Dictionary<string, List<string>>>();
  11.  
  12.         string inputLine = Console.ReadLine();
  13.  
  14.         while (inputLine != "Time for Code")
  15.         {
  16.             string[] splitInput = inputLine.Split();
  17.             bool isIDnumber = CheckIDifNumber(splitInput);
  18.             bool hasHashAndAt = CheckForHashAndAt(splitInput);
  19.  
  20.             if (isIDnumber && hasHashAndAt)
  21.             {
  22.                 int ID = int.Parse(splitInput[0]);
  23.                 string eventName = splitInput[1].Substring(1);
  24.                 List<string> names = GetNames(splitInput);
  25.  
  26.                 if (!eventDict.ContainsKey(ID))
  27.                 {
  28.                     eventDict[ID] = new Dictionary<string, List<string>>();
  29.  
  30.                     if (!eventDict[ID].ContainsKey(eventName))
  31.                     {
  32.                         eventDict[ID].Add(eventName, names);
  33.                     }
  34.                 }
  35.                 else
  36.                 {
  37.                     if (eventDict[ID].ContainsKey(eventName))
  38.                     {
  39.                         eventDict[ID][eventName].AddRange(names);
  40.                     }
  41.                 }                
  42.             }
  43.             inputLine = Console.ReadLine();
  44.         }
  45.  
  46.         Dictionary<string, List<string>> resultDictionary =
  47.             new Dictionary<string, List<string>>();
  48.  
  49.         foreach (var IdEvent in eventDict)
  50.         {
  51.             foreach (var eventPeople in IdEvent.Value)
  52.             {
  53.                 List<string> names = eventPeople.Value.Distinct().ToList();
  54.                 names.Sort();
  55.                 if (!resultDictionary.ContainsKey(eventPeople.Key))
  56.                 {
  57.                     resultDictionary[eventPeople.Key] = new List<string>();
  58.                 }
  59.                 resultDictionary[eventPeople.Key].AddRange(names);
  60.             }
  61.         }
  62.  
  63.         foreach (var eventPeople in resultDictionary.
  64.             OrderBy(val => -val.Value.Count).ThenBy(eventName => eventName.Key))
  65.         {
  66.             string eventName = eventPeople.Key;
  67.             int participantsCount = eventPeople.Value.Count;
  68.  
  69.             Console.WriteLine(eventName + " - " + participantsCount);
  70.  
  71.             for (int i = 0; i < eventPeople.Value.Count; i++)
  72.             {
  73.                 Console.WriteLine(eventPeople.Value[i]);
  74.             }
  75.         }
  76.     } //END MAIN
  77.  
  78.     //------------------------METHODS------------------------//
  79.     // Put NAMES into array
  80.     private static List<string> GetNames(string[] splitInput)
  81.     {
  82.         List<string> names = new List<string>();
  83.         for (int i = 2; i < splitInput.Length; i++)
  84.         {
  85.             names.Add(splitInput[i]);
  86.         }
  87.         names = names.Distinct().ToList();
  88.         return names;
  89.     }
  90.  
  91.  
  92.     // Check if event and names start with # and @
  93.     private static bool CheckForHashAndAt(string[] splitInput)
  94.     {
  95.         string eventName = splitInput[1];
  96.         bool hashTagEvent = eventName[0] == '#' ? true : false;
  97.  
  98.         bool atName = true;
  99.         for (int i = 2; i < splitInput.Length; i++)
  100.         {
  101.             string name = splitInput[i];
  102.             atName = name[0] == '@' ? true : false;
  103.             if (atName == false)
  104.             {
  105.                 break;
  106.             }
  107.         }
  108.  
  109.         bool checkInputFormat = hashTagEvent && atName == true ? true : false;
  110.         return checkInputFormat;
  111.     }
  112.  
  113.     // Check if ID is a number, not a letter
  114.     static bool CheckIDifNumber(string[] splitInput)
  115.     {
  116.         bool isItNumber = true;
  117.         try
  118.         {
  119.             int ID = int.Parse(splitInput[0]);
  120.         }
  121.         catch
  122.         {
  123.             isItNumber = false;
  124.         }
  125.         return isItNumber;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement