Advertisement
simonradev

Files

Feb 16th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Files
  6. {
  7.     public class Files
  8.     {
  9.         public static void Main()
  10.         {
  11.             int numberOfRoots = int.Parse(Console.ReadLine());
  12.  
  13.             Dictionary<string, Dictionary<string, long>> allDirectories =
  14.                             new Dictionary<string, Dictionary<string, long>>();
  15.  
  16.             for (int currRoot = 0; currRoot < numberOfRoots; currRoot++)
  17.             {
  18.                 string[] inputLine = Console.ReadLine().Split(';');
  19.                 string[] directory = inputLine[0].Split('\\');
  20.  
  21.                 string folderName = directory[0];
  22.                 string fileName = directory[directory.Length - 1];
  23.                 long fileSize = long.Parse(inputLine[inputLine.Length - 1]);
  24.  
  25.                 if (!allDirectories.ContainsKey(folderName))
  26.                 {
  27.                     allDirectories.Add(folderName, new Dictionary<string, long>());
  28.                 }
  29.  
  30.                 allDirectories[folderName][fileName] = fileSize;
  31.             }
  32.  
  33.  
  34.             string[] searchInfo = Console.ReadLine().Split();
  35.             string extensionToSearch = searchInfo[0];
  36.             string folderToSearchIn = searchInfo[2];
  37.  
  38.             if (!allDirectories.ContainsKey(folderToSearchIn))
  39.             {
  40.                 Console.WriteLine("No");
  41.  
  42.                 return;
  43.             }
  44.  
  45.             bool thereIsMatch = false;
  46.             foreach (KeyValuePair<string, long> fileAndSize in allDirectories[folderToSearchIn]
  47.                                                                     .OrderByDescending(f => f.Value)
  48.                                                                     .ThenBy(n => n.Key))
  49.             {
  50.                 string[] fileInfo = fileAndSize.Key.Split('.');
  51.                 string fileExtension = fileInfo[1];
  52.  
  53.                 if (extensionToSearch.Equals(fileExtension))
  54.                 {
  55.                     thereIsMatch = true;
  56.                     Console.WriteLine($"{fileAndSize.Key} - {fileAndSize.Value} KB");
  57.                 }
  58.             }
  59.  
  60.             if (!thereIsMatch)
  61.             {
  62.                 Console.WriteLine("No");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement