Advertisement
a1m

UserLogs

a1m
Oct 2nd, 2015
145
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.Text;
  4.  
  5. namespace UserLogs
  6. {
  7.     class UserLogs
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var userLogs = new SortedDictionary<string,Dictionary<string, int>> ();
  12.             string input = Console.ReadLine();
  13.             while (input != "end")
  14.             {  
  15.                 string[] data = input.Split();
  16.                 string ipAddress = data[0].Replace("IP=",string.Empty);
  17.                 string name = data[2].Replace("user=", String.Empty);
  18.                 if (!userLogs.ContainsKey(name))
  19.                 {
  20.                     userLogs.Add(name,new Dictionary<string, int>());
  21.                     userLogs[name].Add(ipAddress,1);
  22.                 }
  23.                 else if (!userLogs[name].ContainsKey(ipAddress))
  24.                 {
  25.                     userLogs[name].Add(ipAddress,1);
  26.                 }
  27.                 else
  28.                 {
  29.                     userLogs[name][ipAddress] ++;
  30.                 }
  31.                 input = Console.ReadLine();
  32.             }
  33.  
  34.             foreach (string user in userLogs.Keys)
  35.             {
  36.                 StringBuilder result = new StringBuilder();
  37.                 result.AppendFormat("{0}:{1}", user,Environment.NewLine);
  38.  
  39.                 foreach (var ip in userLogs[user])
  40.                 {
  41.                     result.Append(ip.Key + " => " + ip.Value+ ", ");
  42.                 }
  43.  
  44.                 Console.WriteLine(result.ToString().Trim(new char[] {' ',','}) + ".");
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement