Advertisement
stefanMinch3v

SoftUni/ProgrammingF/Exam-1

Mar 9th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Practice
  9. {
  10.     public class Practice
  11.     {
  12.         public static void Main(string[] args)
  13.         {
  14.             int num = int.Parse(Console.ReadLine());
  15.             Dictionary<string, Dictionary<string, decimal>> files = new Dictionary<string, Dictionary<string, decimal>>();
  16.  
  17.             string patternFirst = @"^[^\\]*";//catches the first letters regard directory
  18.             string patternKey = @"(\w[^\\]*);";//catches the file name
  19.             string patternValue = @"(\d+)(?!.*\d)";//catches the extension
  20.  
  21.             for (int i = 0; i < num; i++)
  22.             {
  23.                 string input = Console.ReadLine();
  24.                 string directory = string.Empty;
  25.                 string dictKey = string.Empty;
  26.                 decimal dictValue = 0;
  27.  
  28.                 Regex regexFirst = new Regex(patternFirst);
  29.                 Regex regexKey = new Regex(patternKey);
  30.                 Regex regexValue = new Regex(patternValue);
  31.  
  32.                 MatchCollection collection1 = regexFirst.Matches(input);
  33.                 MatchCollection collection2 = regexKey.Matches(input);
  34.                 MatchCollection collection3 = regexValue.Matches(input);
  35.  
  36.                 foreach (Match match in collection1)
  37.                 {
  38.                     directory = match.ToString();
  39.                 }
  40.  
  41.                 foreach (Match match in collection2)
  42.                 {
  43.                     dictKey = match.Groups[1].ToString();
  44.                 }
  45.  
  46.                 foreach (Match match in collection3)
  47.                 {
  48.                     dictValue = decimal.Parse(match.ToString());
  49.                 }
  50.  
  51.                 FillOutDictionary(files, directory, dictKey, dictValue);
  52.             }
  53.  
  54.             string[] checkFile = Console.ReadLine().Split();
  55.  
  56.             string searchDirectory = checkFile[2];
  57.             string searchFile = checkFile[0];
  58.  
  59.             if (files.ContainsKey(searchDirectory))
  60.             {
  61.                 bool found = false;
  62.                 foreach (var item in files[searchDirectory].OrderByDescending(value => value.Value).ThenBy(key => key.Key))
  63.                 {
  64.                     string search = string.Join("",item.Key);
  65.                     if (search.Substring(search.Length - 3).Contains(searchFile))
  66.                     {
  67.                         string keys = string.Join("", item.Key);
  68.                         string values = string.Join("", item.Value);
  69.                         Console.WriteLine($"{keys} - {values} KB");
  70.                         found = true;
  71.                     }
  72.                 }
  73.                 if (found == false)
  74.                 {
  75.                     Console.WriteLine("No");
  76.                 }                                      
  77.             }
  78.             else
  79.             {
  80.                 Console.WriteLine("No");
  81.             }
  82.         }
  83.  
  84.         private static void FillOutDictionary(Dictionary<string, Dictionary<string, decimal>> files, string directory, string dictKey, decimal dictValue)
  85.         {
  86.             if (!files.ContainsKey(directory))
  87.             {
  88.                 files.Add(directory, new Dictionary<string, decimal>());
  89.                 files[directory].Add(dictKey, dictValue);
  90.             }
  91.             else
  92.             {
  93.                 if (!files[directory].ContainsKey(dictKey))
  94.                 {
  95.                     files[directory].Add(dictKey, dictValue);
  96.                 }
  97.                 else
  98.                 {
  99.                     files[directory][dictKey] = dictValue;
  100.                 }
  101.             }
  102.             if (files[directory].ContainsKey(dictKey))
  103.             {
  104.                 files[directory][dictKey] = dictValue;
  105.             }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement