Advertisement
VasilKotsev

06-User logs

Jun 9th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. namespace _06_User_logs
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Net;
  7.     using System.Text.RegularExpressions;
  8.  
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.  
  14.             // Judge LINK: https://judge.softuni.bg/Contests/Compete/Index/209#5
  15.             var logsDict = new SortedDictionary<string, Dictionary<string, int>>();
  16.  
  17.             while (true)
  18.             {
  19.                 string[] inputCommand = Console.ReadLine()
  20.                     .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  21.                     .ToArray();
  22.  
  23.                 if (inputCommand[0] == "end")
  24.                 {
  25.                     break;
  26.                 }
  27.  
  28.                 else
  29.                 {
  30.                     //string regexValidateIPv4 = @"'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b'";
  31.                     string currentAddress = string.Join("", inputCommand[0]
  32.                         .ToCharArray()
  33.                         .Skip(3)
  34.                         .ToArray());
  35.  
  36.                     string currentName = string.Join("", inputCommand[2]
  37.                         .ToCharArray()
  38.                         .Skip(5)
  39.                         .ToArray());
  40.  
  41.                     bool isCurrentAddressValid = CheckAddress(currentAddress);
  42.  
  43.                     // Holds the current log address
  44.                     var addressesLogs = new Dictionary<string, int>();
  45.  
  46.                     if (isCurrentAddressValid == true)
  47.                     {
  48.                         addressesLogs.Add(currentAddress, 1);
  49.  
  50.                         // If user is not present, add him.
  51.                         if (logsDict.ContainsKey(currentName) == false)
  52.                         {
  53.                             logsDict.Add(currentName, addressesLogs);
  54.                         }
  55.  
  56.                         else
  57.                         {
  58.                             // Check if user has logged from different addresses in order to add them.
  59.                             if (logsDict[currentName].ContainsKey(currentAddress) == false)
  60.                             {
  61.                                 logsDict[currentName].Add(currentAddress, 1);
  62.                             }
  63.  
  64.                             // If not he has logged from previous IP.
  65.                             else
  66.                             {
  67.                                 logsDict[currentName][currentAddress]++;
  68.                             }
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             // PRINT
  75.             foreach (var user in logsDict)
  76.             {
  77.                 Console.WriteLine($"{user.Key}:");
  78.                 var addresses = user.Value;
  79.  
  80.                 var lastAddress = addresses.Last();
  81.  
  82.                 foreach (var address in addresses)
  83.                 {
  84.  
  85.                     if (address.Equals(lastAddress))
  86.                     {
  87.                         Console.WriteLine($"{address.Key} => {address.Value}.");
  88.                     }
  89.  
  90.                     else
  91.                     {
  92.                         Console.Write($"{address.Key} => {address.Value}, ");
  93.                     }
  94.                 }
  95.  
  96.             }
  97.         }
  98.  
  99.         static bool CheckAddress(string address)
  100.         {
  101.             IPAddress validAddress;
  102.             bool checkIfIPv6 = false;
  103.             bool checkIfIPv4 = false;
  104.  
  105.             if (IPAddress.TryParse(address, out validAddress) == true)
  106.             {
  107.                 checkIfIPv6 = validAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6;
  108.                 checkIfIPv4 = validAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;
  109.  
  110.                 // This maybe pointless... If parse is successful better to return TRUE ?
  111.                 if (checkIfIPv4 == true || checkIfIPv6 == true)
  112.                 {
  113.                     return true;
  114.                 }
  115.  
  116.                 return false;
  117.             }
  118.  
  119.             return false;
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement