Advertisement
v4m4v4

User Logs

Aug 1st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class UserLogs
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         var usersParameters = new Dictionary<string, Dictionary<string, int>>();
  10.         string input = Console.ReadLine();
  11.  
  12.         while (input != "end")
  13.         {
  14.             string[] inputTokens = input
  15.                 .Split(new char[] { '=', ' ' },
  16.                     StringSplitOptions
  17.                     .RemoveEmptyEntries);
  18.  
  19.             string ip = inputTokens[1];
  20.             string userName = inputTokens[5];
  21.  
  22.             if (!usersParameters.ContainsKey(userName))
  23.             {
  24.                 usersParameters.Add(userName, new Dictionary<string, int>());
  25.             }
  26.  
  27.             if (!usersParameters[userName].ContainsKey(ip))
  28.             {
  29.                 usersParameters[userName].Add(ip, 0);
  30.             }
  31.  
  32.             usersParameters[userName][ip]++;
  33.  
  34.             input = Console.ReadLine();
  35.         }
  36.  
  37.         foreach (var itemUser in usersParameters.OrderBy(x => x.Key))
  38.         {
  39.             string userName = itemUser.Key;
  40.             Dictionary<string, int> usersIP = itemUser.Value;
  41.             List<string> helper = new List<string>();
  42.  
  43.             Console.WriteLine($"{userName}:");
  44.  
  45.             foreach (var itemIP in usersIP)
  46.             {
  47.                 string ip = itemIP.Key;
  48.                 int count = itemIP.Value;
  49.  
  50.                 helper.Add($"{ip} => {count}");      
  51.             }
  52.  
  53.             Console.WriteLine(string.Join(", ", helper) + ".");
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement