Advertisement
Nikolay_Kashev

Logs Aggregator

May 18th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _08._Logs_Aggregator
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int numOfLogs = int.Parse(Console.ReadLine());
  12.             SortedDictionary<string, SortedDictionary<string, int>> dictOfUsers = new SortedDictionary<string, SortedDictionary<string, int>>();
  13.            
  14.             for (int i = 0; i < numOfLogs; i++)
  15.             {
  16.                 var userInfo = Console.ReadLine().Split().ToArray();
  17.                 string IP = userInfo[0];
  18.                 string username = userInfo[1];
  19.                 int duration = int.Parse(userInfo[2]);
  20.                
  21.                 if (!dictOfUsers.ContainsKey(username))
  22.                 {
  23.                     dictOfUsers.Add(username, new SortedDictionary<string, int>());
  24.                 }
  25.                 if (!dictOfUsers[username].ContainsKey(IP))
  26.                 {
  27.                     dictOfUsers[username].Add(IP, duration);
  28.                 }
  29.                 else
  30.                 {
  31.                     dictOfUsers[username][IP] += duration;
  32.                 }
  33.  
  34.             }
  35.  
  36.             foreach (var user in dictOfUsers)
  37.             {
  38.                 var totalDurationOfUser = dictOfUsers[user.Key].Values.Sum();
  39.                 var listOfIps = user.Value.Keys.ToList();
  40.                 Console.WriteLine($"{user.Key}: {totalDurationOfUser} [{string.Join(", ", listOfIps)}]");
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement