TonyTroev

Files

Mar 3rd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _04_04_Files
  7. {
  8.     class Files
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // 100% from the very first time!
  13.             Regex rootPattern = new Regex(@"^(?<root>.+?)\\");
  14.             Regex namePattern = new Regex(@"^.*\\(?<fileName>.+\..+?)$");
  15.             Dictionary<string, Dictionary<string, long>> files = new Dictionary<string, Dictionary<string, long>>();
  16.             long n = long.Parse(Console.ReadLine());
  17.  
  18.             for (long i = 0; i < n; i++)
  19.             {
  20.                 string[] data = Console.ReadLine()
  21.                 .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  22.  
  23.                 string path = data[0];
  24.                 long size = long.Parse(data[1]);
  25.                 string rootName = rootPattern.Match(path).Groups["root"].Value;
  26.                 string fileName = namePattern.Match(path).Groups["fileName"].Value;
  27.  
  28.                 if (!files.ContainsKey(rootName))
  29.                 {
  30.                     files.Add(rootName, new Dictionary<string, long>());
  31.                 }
  32.                 if (!files[rootName].ContainsKey(fileName))
  33.                 {
  34.                     files[rootName].Add(fileName, 0);
  35.                 }
  36.                 files[rootName][fileName] = size;
  37.             }
  38.  
  39.             string[] query = Console.ReadLine()
  40.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  41.             string extension = query[0];
  42.             string root = query[2];
  43.  
  44.             if (!files.ContainsKey(root))
  45.             {
  46.                 Console.WriteLine("No");
  47.             }
  48.             else
  49.             {
  50.                 Dictionary<string, long> queryFiles = files[root].Where(x => x.Key.EndsWith(extension)).ToDictionary(x => x.Key, x => x.Value);
  51.                 if (queryFiles.Count == 0)
  52.                 {
  53.                     Console.WriteLine("No");
  54.                 }
  55.                 else
  56.                 {
  57.                     foreach (KeyValuePair<string, long> file in queryFiles.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  58.                     {
  59.                         Console.WriteLine($"{file.Key} - {file.Value} KB");
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment