Advertisement
_CodeBehind

6. * User Logs

Feb 5th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         var line = Console.ReadLine().Split(' ');
  10.  
  11.         var users = new SortedDictionary<string, Dictionary<string, int>>();
  12.  
  13.         while (line[0] != "end")
  14.         {
  15.             var user = line[2].Split('=');
  16.             var ip = line[0].Split('=');
  17.  
  18.             var userName = user[1];
  19.             var ipAdress = ip[1];
  20.  
  21.             line = Console.ReadLine().Split(' ');
  22.  
  23.             InsertName(users, userName);
  24.             InsertIP(users, userName, ipAdress);
  25.         }
  26.  
  27.         foreach (var user in users)
  28.         {
  29.             Console.WriteLine($"{user.Key}:");
  30.             Console.WriteLine(string.Join(", ", user.Value.Select(value => value.Key + " => " + value.Value).ToArray()) + ".");
  31.         }
  32.     }
  33.  
  34.     private static void InsertName(SortedDictionary<string, Dictionary<string, int>> users, string username)
  35.     {
  36.         if (!users.ContainsKey(username))
  37.         {
  38.             users[username] = new Dictionary<string, int>();
  39.         }
  40.     }
  41.  
  42.     private static void InsertIP(SortedDictionary<string, Dictionary<string, int>> users, string username, string ipAdress)
  43.     {
  44.         if (!users[username].ContainsKey(ipAdress))
  45.         {
  46.             users[username][ipAdress] = 0;
  47.         }
  48.         users[username][ipAdress] += 1;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement