Advertisement
YavorGrancharov

Logs_Aggregator(dict)

Oct 20th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Logs_Aggregator
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             SortedDictionary<string, Dictionary<string, int>> data =
  14.                 new SortedDictionary<string, Dictionary<string, int>>();
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 string input = Console.ReadLine();
  19.  
  20.                 string[] tokens = input.Split(' ').ToArray();
  21.                 string ip = tokens[0];
  22.                 string name = tokens[1];
  23.                 int duration = int.Parse(tokens[2]);
  24.  
  25.                 if (!data.ContainsKey(name))
  26.                 {
  27.                     data.Add(name, new Dictionary<string, int>());
  28.                 }
  29.                 if (!data[name].ContainsKey(ip))
  30.                 {
  31.                     data[name][ip] = duration;
  32.                 }
  33.                 else
  34.                 {
  35.                     data[name][ip] += duration;
  36.                 }
  37.             }
  38.             foreach (var entry in data)
  39.             {
  40.                 string name = entry.Key;
  41.                 int total = entry.Value.Values.Sum();
  42.                 List<string> ips = entry.Value.Keys.OrderBy(x => x).Distinct().ToList();
  43.                 Console.WriteLine("{0}: {1} [{2}]", name, total, string.Join(", ", ips));
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement