Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _07b_Dictionaries_Lambda_Expressions_LINQ_Exercises
- {
- class Dictionaries_Lambda_Expressions_LINQ_Exercises
- {
- static void Main(string[] args)
- {
- Dictionary<string, Dictionary<string, int>> logs = new Dictionary<string, Dictionary<string, int>>();
- int n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- string line = Console.ReadLine();
- string[] tokens = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
- string ip = tokens[0];
- string user = tokens[1];
- int duration = int.Parse(tokens[2]);
- if (!logs.ContainsKey(user))
- {
- logs.Add(user, new Dictionary<string, int>());
- }
- if (!logs[user].ContainsKey(ip))
- {
- logs[user].Add(ip, duration);
- }
- else
- {
- logs[user][ip] += duration;
- }
- }
- foreach (var log in logs.OrderBy(x => x.Key))
- {
- string username = log.Key;
- int totalDuration = log.Value.Sum(x => x.Value);
- string[] ips = log.Value
- .Keys
- .OrderBy(x => x)
- .ToArray();
- Console.WriteLine($"{username}: {totalDuration} [{string.Join(", ", ips)}]");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement