Advertisement
YavorGrancharov

User_Logs(dict)

Oct 20th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace User_Logs
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.  
  13.             Dictionary<string, Dictionary<string, int>> data =
  14.                 new Dictionary<string, Dictionary<string, int>>();
  15.  
  16.             int count = 1;
  17.             while (input != "end")
  18.             {
  19.                 string[] tokens = input
  20.                     .Split(new string[] { "IP", "=", " ", "message", "'&'", "user" },
  21.                     StringSplitOptions.RemoveEmptyEntries);
  22.  
  23.                 string ip = tokens[0];
  24.                 string message = tokens[1];
  25.                 string user = tokens[2];
  26.                
  27.                 if (!data.ContainsKey(user))
  28.                 {
  29.                     data.Add(user, new Dictionary<string, int>());
  30.                 }
  31.                 Dictionary<string, int> adress = data[user];
  32.  
  33.                 if (!adress.ContainsKey(ip))
  34.                 {
  35.                     adress[ip] = count;
  36.                 }
  37.                 else
  38.                 {
  39.                     adress[ip] += count;
  40.                 }
  41.                 input = Console.ReadLine();
  42.             }
  43.  
  44.             foreach (var entry in data.OrderBy(x => x.Key).ThenBy(x => x.Value))
  45.             {
  46.                 string user = entry.Key;
  47.                 Console.WriteLine("{0}: ",user);
  48.                 Dictionary<string, int> countMessages = entry.Value;
  49.                 foreach (var record in countMessages)
  50.                 {
  51.                     string ip = record.Key;
  52.                     int cnt = record.Value;
  53.                     if (record.Equals(entry.Value.Last()))
  54.                     {
  55.                         Console.WriteLine("{0} => {1}.",ip,cnt);
  56.                         break;
  57.                     }
  58.                     Console.Write("{0} => {1}, ",ip, cnt);
  59.                 }  
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement