Advertisement
Nikolay_Kashev

User Logs

May 18th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _06.User_Logs
  6. {
  7. class Dictionaries
  8.   {
  9.     static void Main(string[] args)
  10.     {
  11.         SortedDictionary<string, List<string>> users = new SortedDictionary<string, List<string>>();
  12.         string command = "";
  13.        
  14.         while (true)
  15.         {
  16.             List<string> entry = Console.ReadLine().Split().ToList();
  17.             command = entry[0];
  18.            
  19.             if (command == "end") break;
  20.            
  21.             int indexOfIP = command.IndexOf('=') + 1;
  22.             string ip = command.Substring(indexOfIP);
  23.             int indexOfUser = entry[2].LastIndexOf('=') + 1;
  24.             string user = entry[2].Substring(indexOfUser);
  25.             List<string> IPs = new List<string>();
  26.             IPs.Add(ip);
  27.            
  28.             if (!users.ContainsKey(user))
  29.             {
  30.                 users[user] = IPs;
  31.             }
  32.             else
  33.             {
  34.                 users[user].AddRange(IPs);
  35.             }
  36.         }
  37.  
  38.         foreach (var user in users)
  39.         {
  40.             Console.WriteLine(user.Key + ": ");
  41.             List<string> IPs = user.Value;
  42.             Dictionary<string, int> numberOfIPs = new Dictionary<string, int>();
  43.            
  44.             foreach (var ip in IPs)
  45.             {
  46.                 if (!numberOfIPs.ContainsKey(ip))
  47.                 {
  48.                     numberOfIPs[ip] = 1;
  49.                 }
  50.                 else
  51.                 {
  52.                     numberOfIPs[ip] += 1;
  53.                 }
  54.             }
  55.  
  56.             int count = numberOfIPs.Count;
  57.            
  58.             foreach (var ip in numberOfIPs)
  59.             {
  60.                 count--;
  61.                
  62.                 if (count > 0)
  63.                 {
  64.                     Console.Write(ip.Key + " => " + ip.Value + ", ");
  65.                 }
  66.                 else
  67.                 {
  68.                     Console.Write(ip.Key + " => " + ip.Value + ". ");
  69.                 }
  70.             }
  71.             Console.WriteLine();
  72.  
  73.         }
  74.     }
  75.   }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement