Aliendreamer

user logs

Aug 22nd, 2018
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. namespace nestedDictionaries
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.  
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.          
  13.             SortedDictionary<string,Dictionary<string,int>>usersAndIps=new SortedDictionary<string, Dictionary<string, int>>();
  14.  
  15.             string input;
  16.  
  17.             while ((input=Console.ReadLine())!="end")
  18.             {
  19.                 if (input == null) continue;
  20.                 string[] tokens = input.Split(new[]{'=',' '}, StringSplitOptions.RemoveEmptyEntries).ToArray();
  21.  
  22.                 string ip = tokens[1];
  23.                 string user = tokens[5];
  24.  
  25.                 if (!usersAndIps.ContainsKey(user))
  26.                 {
  27.                     usersAndIps.Add(user,new Dictionary<string, int>());
  28.                     usersAndIps[user].Add(ip,1);
  29.                 }
  30.                 else if(!usersAndIps[user].ContainsKey(ip))
  31.                 {
  32.                     usersAndIps[user].Add(ip, 1);
  33.                 }
  34.                 else
  35.                 {
  36.                     usersAndIps[user][ip] += 1;
  37.                 }
  38.             }
  39.  
  40.             StringBuilder sb = new StringBuilder();
  41.  
  42.             foreach (var user in usersAndIps)
  43.             {
  44.                 sb.AppendLine($"{user.Key}: ");
  45.                 int count = user.Value.Count;
  46.                 int current = 0;
  47.                 foreach (var ips in user.Value)
  48.                 {
  49.                     current++;
  50.                     string ip = current<count? string.Format("{0} => {1}, ", ips.Key, ips.Value)
  51.                                              : string.Format("{0} => {1}.", ips.Key, ips.Value);
  52.                     sb.Append(ip);
  53.                 }
  54.                
  55.                 Console.WriteLine(sb.ToString());
  56.                 sb.Clear();
  57.             }
  58.          
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment