Advertisement
Guest User

User Logs

a guest
Oct 7th, 2016
471
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. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace problem06_user_logs
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             SortedDictionary<string, Dictionary<string, int>> ips = new SortedDictionary<string, Dictionary<string, int>>();
  14.             string input = Console.ReadLine();
  15.             while(!input.Equals("end"))
  16.             {
  17.                 string[] inputStrings = input.Split(' ');
  18.                 string ip = inputStrings[0].Substring(3);
  19.                 string userName = inputStrings[2].Substring(5);
  20.                 if (!ips.ContainsKey(userName))
  21.                 {
  22.                     ips[userName] = new Dictionary<string, int>();
  23.                     ips[userName].Add(ip, 1);
  24.                 }
  25.                 else
  26.                 {
  27.                     if (ips[userName].ContainsKey(ip))
  28.                     {
  29.                         ips[userName][ip]++;
  30.                     }
  31.                     else
  32.                     {
  33.                         ips[userName].Add(ip, 1);
  34.                     }                  
  35.                 }
  36.                 input = Console.ReadLine();
  37.             }
  38.             printDictionary(ips);
  39.         }
  40.  
  41.         private static void printDictionary(SortedDictionary<string, Dictionary<string, int>> ips)
  42.         {
  43.             foreach(string userName in ips.Keys)
  44.             {
  45.                 Console.WriteLine($"{userName}: ");
  46.                 var ordered = ips[userName].OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
  47.                 string ipString = "";
  48.                 foreach (string ip in ordered.Keys)
  49.                 {
  50.                     ipString += $"{ip} => {ips[userName][ip]}, ";
  51.                 }
  52.                 ipString = ipString.Substring(0, ipString.Length - 2);
  53.                 ipString += ".";
  54.                 Console.WriteLine(ipString);
  55.             }          
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement