Danny_Berova

04. Files

Aug 14th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1.  
  2. namespace _04.Files
  3. {
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     public class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.             var filesByRoot = new Dictionary<string, Dictionary<string, long>>();
  13.             Dictionary<string, string> filesWithExt = new Dictionary<string, string>();
  14.  
  15.             int n = int.Parse(Console.ReadLine());
  16.  
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 string[] tokens = Console.ReadLine().Split('\\');
  20.  
  21.                 string root = tokens[0];
  22.  
  23.                 string[] fileParts = tokens[tokens.Length - 1].Split(';');
  24.  
  25.                 string fileWithExtension = fileParts[0];
  26.                 long fileSize = long.Parse(fileParts[1]);
  27.  
  28.                 string fileName = fileWithExtension;
  29.                 string fileExt = fileName.Split('.')[fileName.Split('.').Length - 1];
  30.  
  31.                 if (!filesByRoot.ContainsKey(root))
  32.                 {
  33.                     filesByRoot[root] = new Dictionary<string, long>();
  34.                 }
  35.  
  36.                 filesByRoot[root][fileName] = fileSize;
  37.                 filesWithExt[fileName] = fileExt;
  38.             }
  39.  
  40.             string[] queryParts = Console.ReadLine().Split(' ');
  41.  
  42.             string queryExtension = queryParts[0];
  43.             string queryRoot = queryParts[2];
  44.  
  45.             Dictionary<string, long> selectedFiles = new Dictionary<string, long>();
  46.  
  47.             if (filesByRoot.ContainsKey(queryRoot))
  48.             {
  49.                 foreach (var file in filesByRoot[queryRoot])
  50.                 {
  51.                     if (filesWithExt[file.Key] == queryExtension)
  52.                     {
  53.                         selectedFiles.Add(file.Key, file.Value);
  54.                     }
  55.                 }
  56.             }
  57.  
  58.             if (selectedFiles.Count > 0)
  59.             {
  60.                 foreach (var file in selectedFiles.OrderByDescending(f => f.Value)
  61.                     .ThenBy(f => f.Key))
  62.                 {
  63.                     Console.WriteLine($"{file.Key} - {file.Value} KB");
  64.                 }
  65.             }
  66.             else
  67.             {
  68.                 Console.WriteLine("No");
  69.             }
  70.         }
  71.     }
  72. }
Add Comment
Please, Sign In to add comment