Advertisement
krasi1105

4.Files

Feb 23rd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8.  
  9. namespace _04.Files
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int n = int.Parse(Console.ReadLine());
  16.             //Regex rgxFilenames = new Regex(@"(?'fullPath'(?'root'[A-Za-z]+)(?:\\?[A-Za-z]+\\)+?(?'fileName'[A-za-z]+\.(?'extension'[A-Za-z]+)));(?'fileSize'\d+)");
  17.             Dictionary<string, long> files = ReadFiles(n);
  18.             string query = Console.ReadLine();
  19.             Regex rgxQuery = new Regex(@"(?'extension'[A-Za-z]+) in (?'root'.+)");
  20.             Match m = rgxQuery.Match(query);
  21.             string extension = m.Groups["extension"].Value;
  22.             string root = m.Groups["root"].Value;
  23.             Dictionary<string, long> filesToPrint = files
  24.                                                   .Where(file => MeetsConditions(file.Key, root, extension))
  25.                                                   .Select(a => RemoveDirectory(a))
  26.                                                   .OrderByDescending(a => a.Value)
  27.                                                   .ThenBy(a => a.Key)
  28.                                                   .ToDictionary(a => a.Key, a => a.Value);
  29.             if (filesToPrint.Count == 0)
  30.             { Console.WriteLine("No"); return; }
  31.             foreach (var file in filesToPrint)
  32.             {
  33.                 Console.WriteLine($"{file.Key} - {file.Value} KB");
  34.             }
  35.         }
  36.  
  37.         private static Dictionary<string, long> ReadFiles(int n)
  38.         {
  39.             Dictionary<string, long> files = new Dictionary<string, long>();
  40.             Regex rgxFiles = new Regex(@"^(?'fullPath'(?'root'[^\\]+)\\(?:[^\\]+\\)*?(?'fileName'[^\\]+\.(?'extension'[^\\]+)));(?'fileSize'[\d]+)$");
  41.             for (int i = 0; i < n; i++)
  42.             {
  43.                 try
  44.                 {
  45.                     string inputLine = Console.ReadLine();
  46.                     Match m = rgxFiles.Match(inputLine);
  47.                     string path = m.Groups["fullPath"].Value;
  48.                     long fileSize = long.Parse(m.Groups["fileSize"].Value);
  49.                     files[path] = fileSize;
  50.                 }
  51.                 catch (Exception)
  52.                 {
  53.                 }
  54.             }
  55.             return files;
  56.         }
  57.  
  58.         private static bool MeetsConditions(string fileName, string root, string extension)
  59.         {
  60.             string fileRoot = fileName.Substring(0, root.Length);
  61.             string fileExtension = GetExtension(fileName);
  62.             if (fileRoot == root && fileExtension == extension)
  63.             {
  64.                 return true;
  65.             }
  66.             return false;
  67.         }
  68.  
  69.         private static string GetExtension(string fileName)
  70.         {
  71.             return new string(fileName.Reverse().TakeWhile(a => a != '.').Reverse().ToArray());
  72.         }
  73.  
  74.         private static KeyValuePair<string, long> RemoveDirectory(KeyValuePair<string, long> a)
  75.         {
  76.             string removed = new string(a.Key.Reverse().TakeWhile(letter => letter != '\\').Reverse().ToArray());
  77.             return new KeyValuePair<string, long>(removed, a.Value);
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement