Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Numerics;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace _04.Files
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- //Regex rgxFilenames = new Regex(@"(?'fullPath'(?'root'[A-Za-z]+)(?:\\?[A-Za-z]+\\)+?(?'fileName'[A-za-z]+\.(?'extension'[A-Za-z]+)));(?'fileSize'\d+)");
- Dictionary<string, long> files = ReadFiles(n);
- string query = Console.ReadLine();
- Regex rgxQuery = new Regex(@"(?'extension'[A-Za-z]+) in (?'root'.+)");
- Match m = rgxQuery.Match(query);
- string extension = m.Groups["extension"].Value;
- string root = m.Groups["root"].Value;
- Dictionary<string, long> filesToPrint = files
- .Where(file => MeetsConditions(file.Key, root, extension))
- .Select(a => RemoveDirectory(a))
- .OrderByDescending(a => a.Value)
- .ThenBy(a => a.Key)
- .ToDictionary(a => a.Key, a => a.Value);
- if (filesToPrint.Count == 0)
- { Console.WriteLine("No"); return; }
- foreach (var file in filesToPrint)
- {
- Console.WriteLine($"{file.Key} - {file.Value} KB");
- }
- }
- private static Dictionary<string, long> ReadFiles(int n)
- {
- Dictionary<string, long> files = new Dictionary<string, long>();
- Regex rgxFiles = new Regex(@"^(?'fullPath'(?'root'[^\\]+)\\(?:[^\\]+\\)*?(?'fileName'[^\\]+\.(?'extension'[^\\]+)));(?'fileSize'[\d]+)$");
- for (int i = 0; i < n; i++)
- {
- try
- {
- string inputLine = Console.ReadLine();
- Match m = rgxFiles.Match(inputLine);
- string path = m.Groups["fullPath"].Value;
- long fileSize = long.Parse(m.Groups["fileSize"].Value);
- files[path] = fileSize;
- }
- catch (Exception)
- {
- }
- }
- return files;
- }
- private static bool MeetsConditions(string fileName, string root, string extension)
- {
- string fileRoot = fileName.Substring(0, root.Length);
- string fileExtension = GetExtension(fileName);
- if (fileRoot == root && fileExtension == extension)
- {
- return true;
- }
- return false;
- }
- private static string GetExtension(string fileName)
- {
- return new string(fileName.Reverse().TakeWhile(a => a != '.').Reverse().ToArray());
- }
- private static KeyValuePair<string, long> RemoveDirectory(KeyValuePair<string, long> a)
- {
- string removed = new string(a.Key.Reverse().TakeWhile(letter => letter != '\\').Reverse().ToArray());
- return new KeyValuePair<string, long>(removed, a.Value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement