Advertisement
Guest User

User Logs

a guest
Oct 23rd, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _06.User_Logs
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var input = Console.ReadLine().Split(' ');
  14.             var userDict = new SortedDictionary<string, List<string>>();
  15.             // keeps usernames as keys and for every user - list of strings with user's IPs
  16.             var ipDict = new Dictionary<string, int>();
  17.             // keeps all the IPs as keys and theit occurencies as values
  18.                                
  19.             while (input[0] != "end")
  20.             {
  21.                 string user = input[2].Substring(5);
  22.                 string IP = input[0].Substring(3);
  23.  
  24.                 if (!userDict.ContainsKey(user)) //new USER
  25.                 {
  26.                     userDict.Add(user, new List<string>());
  27.                     userDict[user].Add(IP);
  28.                     ipDict.Add(IP, 1);
  29.                 }
  30.                 else // existing user : 2 cases
  31.                 {
  32.                     if (ipDict.ContainsKey(IP)) //existing USER,existing IP
  33.                     {
  34.                         ipDict[IP] += 1;
  35.                     }
  36.                     else //existing USER, new IP
  37.                     {
  38.                     userDict[user].Add(IP);
  39.                     ipDict.Add(IP, 1);
  40.                     }
  41.                 }
  42.  
  43.                 input = Console.ReadLine().Split(' ');
  44.             }
  45.             foreach (var user in userDict)
  46.             {
  47.                 string ipAdress = "";
  48.                 List<string> ipRow = new List<string>();
  49.  
  50.                 foreach (var listVal in userDict[user.Key])
  51.                 {
  52.                     ipAdress = listVal + " => " + ipDict[listVal];
  53.                     ipRow.Add(ipAdress);
  54.                 }
  55.                 ipAdress = string.Join(", ",ipRow);
  56.                 Console.WriteLine(user.Key + ":");
  57.                 Console.WriteLine(ipAdress + ".");
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement