Advertisement
dimipan80

User Logs

May 16th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. /* You are given an input in the format:
  2.  * IP=(IP.Address) message=(A&sample&message) user=(username)
  3.  * Your task is to parse the ip and the username from the input and for every user, you have to display every ip from which the corresponding user has sent a message and the count of the messages sent with the corresponding ip. In the output, the usernames must be sorted alphabetically while their IP addresses should be displayed in the order of their first appearance. The output should be in the following format:
  4.  * username:
  5.  * IP => count, IP => count.
  6.  * The input comes from the console as varying number of lines. You have to parse every command until the command that follows is end.
  7.  * The IP addresses must be split with a comma, and each line of IP addresses must end with a dot. */
  8.  
  9. namespace User_Logs
  10. {
  11.     using System;
  12.     using System.Collections.Generic;
  13.     using System.Text.RegularExpressions;
  14.  
  15.     class UserLogs
  16.     {
  17.         private static SortedDictionary<string, Dictionary<string, int>> messagesInfo;
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             messagesInfo = new SortedDictionary<string, Dictionary<string, int>>();
  22.             string input = Console.ReadLine();
  23.             while (input != "end")
  24.             {
  25.                 GetInfoFromInputLine(input);
  26.  
  27.                 input = Console.ReadLine();
  28.             }
  29.  
  30.             PrintResults();
  31.         }
  32.  
  33.         private static void GetInfoFromInputLine(string input)
  34.         {
  35.             const string pattern =
  36. @"IP=\s*([A-Za-z\d.:]+)\s+message=\S*?\s+user=\s*(.{3,50})\s*";
  37.             MatchCollection matches = Regex.Matches(input, pattern);
  38.             string ipAddress = string.Empty, username = string.Empty;
  39.             foreach (Match match in matches)
  40.             {
  41.                 ipAddress = match.Groups[1].Value;
  42.                 username = match.Groups[2].Value;
  43.             }
  44.  
  45.             if (!messagesInfo.ContainsKey(username))
  46.             {
  47.                 messagesInfo[username] = new Dictionary<string, int>();
  48.             }
  49.  
  50.             if (!messagesInfo[username].ContainsKey(ipAddress))
  51.             {
  52.                 messagesInfo[username][ipAddress] = 0;
  53.             }
  54.  
  55.             messagesInfo[username][ipAddress]++;
  56.         }
  57.  
  58.         private static void PrintResults()
  59.         {
  60.             foreach (var username in messagesInfo)
  61.             {
  62.                 Console.WriteLine("{0}:", username.Key);
  63.                 int counter = username.Value.Count;
  64.                 foreach (var ip in username.Value)
  65.                 {
  66.                     counter--;
  67.                     Console.Write("{0} => {1}", ip.Key, ip.Value);
  68.                     if (counter > 0)
  69.                     {
  70.                         Console.Write(", ");
  71.                     }
  72.                 }
  73.  
  74.                 Console.WriteLine(".");
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement