Advertisement
valkata

Files -ExamPreparation ||

Aug 12th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04_files
  8. {
  9.     class files
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             Dictionary<string, Dictionary<string, long>> inputFiles = new Dictionary<string, Dictionary<string, long>>();
  15.            
  16.             while (n-- > 0)
  17.             {
  18.                 string curentFile = Console.ReadLine();
  19.  
  20.                 int index = curentFile.LastIndexOf('\\');
  21.                 curentFile = curentFile.Remove(index, 1).Insert(index, "valkata");
  22.  
  23.                 string[] inputTokkens = curentFile
  24.                     .Split(new string[] { "valkata" }, StringSplitOptions.RemoveEmptyEntries)
  25.                     .ToArray();
  26.  
  27.                 string directory = inputTokkens[0];
  28.  
  29.                 string[] file = inputTokkens[1].Split(';').ToArray();
  30.  
  31.                 string fileName = file[0];
  32.  
  33.                 long fileSize = long.Parse(file[1]);
  34.  
  35.                 if (!inputFiles.ContainsKey(directory))
  36.                 {
  37.                     inputFiles.Add(directory, new Dictionary<string, long>());
  38.                 }
  39.                 if (!inputFiles[directory].ContainsKey(fileName))
  40.                 {
  41.                     inputFiles[directory].Add(fileName, fileSize);
  42.                    
  43.                 }
  44.                 else
  45.                 {
  46.                     inputFiles[directory][fileName] = fileSize;
  47.                    
  48.                 }
  49.             }
  50.             string[] input = Console.ReadLine().Split().ToArray();
  51.             string searchedDirectory = input[2];
  52.             string extension = input[0];
  53.  
  54.             Dictionary<string, long> searchedFiles = new Dictionary<string, long>();
  55.            
  56.             foreach (var file in inputFiles)
  57.             {
  58.                 if (file.Key.Contains(searchedDirectory + "\\"))
  59.                 {
  60.                     var values = file.Value;
  61.                     foreach (var value in values)
  62.                     {
  63.                         if(value.Key.Contains("." + extension))
  64.                         {
  65.                             searchedFiles.Add(value.Key, value.Value);
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.  
  71.             if(searchedFiles.Count == 0)
  72.             {
  73.                 Console.WriteLine("No");
  74.             }
  75.             else
  76.             {
  77.                 var outputFiles = searchedFiles
  78.                     .OrderByDescending(o => o.Value)
  79.                     .ThenBy(o => o.Key)
  80.                     .ToDictionary(x => x.Key, y => y.Value);
  81.                 foreach (var file in outputFiles)
  82.                 {
  83.                     Console.WriteLine($"{file.Key} - {file.Value} KB");
  84.                 }
  85.             }
  86.         }
  87.     }  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement