Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace nestedDictionaries
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- class Program
- {
- static void Main(string[] args)
- {
- SortedDictionary<string,Dictionary<string,int>>usersAndIps=new SortedDictionary<string, Dictionary<string, int>>();
- string input;
- while ((input=Console.ReadLine())!="end")
- {
- if (input == null) continue;
- string[] tokens = input.Split(new[]{'=',' '}, StringSplitOptions.RemoveEmptyEntries).ToArray();
- string ip = tokens[1];
- string user = tokens[5];
- if (!usersAndIps.ContainsKey(user))
- {
- usersAndIps.Add(user,new Dictionary<string, int>());
- usersAndIps[user].Add(ip,1);
- }
- else if(!usersAndIps[user].ContainsKey(ip))
- {
- usersAndIps[user].Add(ip, 1);
- }
- else
- {
- usersAndIps[user][ip] += 1;
- }
- }
- StringBuilder sb = new StringBuilder();
- foreach (var user in usersAndIps)
- {
- sb.AppendLine($"{user.Key}: ");
- int count = user.Value.Count;
- int current = 0;
- foreach (var ips in user.Value)
- {
- current++;
- string ip = current<count? string.Format("{0} => {1}, ", ips.Key, ips.Value)
- : string.Format("{0} => {1}.", ips.Key, ips.Value);
- sb.Append(ip);
- }
- Console.WriteLine(sb.ToString());
- sb.Clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment