Advertisement
Guest User

User Logs

a guest
Oct 2nd, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. class UserLogs
  7. {
  8. static void Main()
  9. {
  10. string inputLine = Console.ReadLine();
  11. StringBuilder iPLogs = new StringBuilder();
  12. SortedDictionary<string, Dictionary<string, int>> output = new SortedDictionary<string, Dictionary<string, int>>();
  13.  
  14. while (inputLine != "end")
  15. {
  16. iPLogs.Append(inputLine);
  17. iPLogs.Append("\n\r");
  18. inputLine = Console.ReadLine();
  19. }
  20.  
  21. Regex regex = new Regex(@"IP=(.+) message=.+ user=(.+)");
  22. MatchCollection matches = regex.Matches(iPLogs.ToString());
  23.  
  24. foreach (Match match in matches)
  25. {
  26. Dictionary<string, int> subDictionary = new Dictionary<string, int>();
  27.  
  28. string iP = match.Groups[1].ToString();
  29. string user = match.Groups[2].ToString();
  30.  
  31. if (output.ContainsKey(user))
  32. {
  33. subDictionary = output[user];
  34. if (subDictionary.ContainsKey(iP))
  35. {
  36. int repetitions = subDictionary[iP] + 1;
  37. subDictionary[iP] = repetitions;
  38. }
  39. else
  40. {
  41. subDictionary.Add(iP, 1);
  42. }
  43.  
  44. output[user] = subDictionary;
  45. }
  46. else
  47. {
  48. subDictionary.Add(iP, 1);
  49. output.Add(user, subDictionary);
  50. }
  51. }
  52.  
  53. foreach (var subDictionary in output)
  54. {
  55. Console.WriteLine("{0}: ", subDictionary.Key);
  56. int counter = 1;
  57. foreach (var ip in subDictionary.Value)
  58. {
  59. Console.Write("{0} => {1}", ip.Key, ip.Value);
  60. if (counter == subDictionary.Value.Count)
  61. {
  62. Console.WriteLine(".");
  63. }
  64. else
  65. {
  66. Console.Write(", ");
  67. }
  68. counter++;
  69. }
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement