Advertisement
martyz

User Logs

Oct 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 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 UserLogs
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var dict = new SortedDictionary<string, Dictionary<string, List<string>>>();
  15.             string input = Console.ReadLine();
  16.             Regex pattern = new Regex(@"(?:IP=|)(?<ip>\S+)\s+(?:message=')(?<message>|\S+)(?:')\s+(?:user=)(?<user>\S+)");
  17.  
  18.             while (input != "end")
  19.             {
  20.                 Match match = pattern.Match(input);
  21.                 string message = match.Groups[2].Value;
  22.                 string user = match.Groups[3].Value;
  23.                 string ip = match.Groups[1].Value;
  24.  
  25.                 if (dict.ContainsKey(user))
  26.                 {
  27.                     if (dict[user].ContainsKey(ip))
  28.                     {
  29.                         dict[user][ip].Add(message);
  30.                     }
  31.                     else
  32.                     {
  33.                         dict[user].Add(ip, new List<string>() {message} );
  34.                     }
  35.                 }
  36.                 else
  37.                 {
  38.                     dict.Add(user, new Dictionary<string, List<string>>());
  39.                     dict[user].Add(ip, new List<string> { message });
  40.                 }
  41.  
  42.                 input = Console.ReadLine(); // new input
  43.  
  44.             }
  45.  
  46.             //Print dict
  47.             foreach (var item in dict)
  48.             {
  49.                 Console.WriteLine(item.Key + ":");
  50.                 Console.WriteLine(String.Join(", ", item.Value.Select(x => x.Key + " => " + x.Value.Count)) + ".");
  51.  
  52.             }
  53.  
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement