Advertisement
butoff

EmailStat

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