NastySwipy

Strings & Regular Expressions - 06. Email Statistics

May 30th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace _10b_Regular_Expressions_RegEx_Exercises
  8. {
  9.     class Regular_Expressions_RegEx_Exercises
  10.     {
  11.       static void Main(string[] args)
  12.         {
  13.             int count = int.Parse(Console.ReadLine());
  14.             Dictionary<string, List<string>> mailList = new Dictionary<string, List<string>>();
  15.  
  16.             for (int i = 0; i < count; i++)
  17.             {
  18.                 string pattern = @"^(?<username>[a-zA-Z]{5,})@(?<domain>(?<mailServer>[a-z]{3,})(?<topDomain>\.com|\.bg|\.org))$";
  19.                 string mail = Console.ReadLine();
  20.                 Match match = Regex.Match(mail, pattern);
  21.  
  22.                 if (match.Success)
  23.                 {
  24.                     string mailServer = match.Groups["domain"].Value;
  25.                     string user = match.Groups["username"].Value;
  26.  
  27.                     if (!mailList.ContainsKey(mailServer))
  28.                     {
  29.                         mailList[mailServer] = new List<string>();
  30.                     }
  31.                     if (!mailList[mailServer].Contains(user))
  32.                     {
  33.                         mailList[mailServer].Add(user);
  34.                     }
  35.                 }
  36.             }
  37.  
  38.             foreach (var item in mailList.OrderByDescending(x => x.Value.Count))
  39.             {
  40.                   Console.WriteLine($"{item.Key}:");
  41.                     foreach (var item2 in item.Value)
  42.                     {
  43.                         Console.WriteLine($"### {item2}");
  44.                     }
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment