Aliendreamer

user logs

Feb 17th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 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<string,Dictionary<string,int>> allusers=new Dictionary<string, Dictionary<string, int>>();
  10.  
  11.  
  12.             string input;
  13.             while ((input=Console.ReadLine())!="end")
  14.             {
  15.                Users(input,allusers);
  16.                
  17.             }
  18.            PrintResult(allusers);
  19.            
  20.         }
  21.         public static void Users(string input, Dictionary<string, Dictionary<string, int>> allusers)
  22.         {
  23.             string[] tokens = input.Split(new[] { ' ', '=' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  24.             string ip = tokens[1];
  25.             string user = tokens[5];
  26.             if (!allusers.ContainsKey(user))
  27.             {
  28.                 allusers.Add(user, new Dictionary<string, int>());
  29.                 allusers[user].Add(ip, 1);
  30.             }
  31.             else
  32.             {
  33.                 if (!allusers[user].ContainsKey(ip))
  34.                 {
  35.                     allusers[user].Add(ip, 1);
  36.                 }
  37.                 else
  38.                 {
  39.                     allusers[user][ip] += 1;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         public static void PrintResult(Dictionary<string, Dictionary<string, int>> allusers)
  45.         {
  46.         var result = allusers.OrderBy(x => x.Key);
  47.         foreach (var user in result)
  48.             {
  49.  
  50.                 Console.WriteLine($"{user.Key}:");
  51.                 int count = user.Value.Count;
  52.                 foreach (var details in user.Value)
  53.                 {
  54.                     count--;
  55.                     if (count == 0)
  56.                     {
  57.                         Console.WriteLine($"{details.Key} => {details.Value}.");
  58.  
  59.                     }
  60.                     Console.WriteLine($"{details.Key} => {details.Value},");
  61.                 }
  62.             }
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment