Advertisement
fbinnzhivko

Untitled

Jan 24th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. class AnonymousCache
  5. {
  6.     static void Main()
  7.     {
  8.         Dictionary<string, Dictionary<string, long>> data = new Dictionary<string, Dictionary<string, long>>();
  9.         Dictionary<string, Dictionary<string, long>> cache = new Dictionary<string, Dictionary<string, long>>();
  10.  
  11.         string inputLine = string.Empty;
  12.  
  13.         while ((inputLine = Console.ReadLine()) != "thetinggoesskrra")
  14.         {
  15.             string[] inputParameters = inputLine.Split(new[] { ' ', '-', '>', '|' }, StringSplitOptions.RemoveEmptyEntries);
  16.  
  17.             if (inputParameters.Length == 1)  
  18.             {
  19.                 string dataSet = inputParameters[0];
  20.                
  21.                 if (cache.ContainsKey(dataSet))
  22.                 {
  23.                     data[dataSet] = new Dictionary<string, long>(cache[dataSet]);
  24.                     cache.Remove(dataSet);
  25.                 }
  26.                 else
  27.                 {
  28.                     data[dataSet] = new Dictionary<string, long>();
  29.                 }
  30.             }
  31.             else
  32.             {
  33.                 string dataKey = inputParameters[0];
  34.                 long dataSize = long.Parse(inputParameters[1]);
  35.                 string dataSet = inputParameters[2];
  36.  
  37.                 //If the data does not contain such a dataSet we add it to the cache
  38.                 if (!data.ContainsKey(dataSet))
  39.                 {
  40.                     //If the cache does not contain such a dataSet, we initialize the inner dictionary
  41.                     //Otherwise we get null reference exception
  42.                     if (!cache.ContainsKey(dataSet))
  43.                     {
  44.                         cache[dataSet] = new Dictionary<string, long>();
  45.                     }
  46.  
  47.                     //Then we just add the key and size to the cache
  48.                     cache[dataSet][dataKey] = dataSize;
  49.                 }
  50.                 else
  51.                 {
  52.                     //If the data contains such a dataSet, we just add the key and the size to it
  53.                     data[dataSet][dataKey] = dataSize;
  54.                 }
  55.             }
  56.         }
  57.         if (data.Count > 0)
  58.         {
  59.             KeyValuePair<string, Dictionary<string, long>> result = data.OrderByDescending(ds => ds.Value.Sum(d => d.Value)).First();
  60.  
  61.             Console.WriteLine($"Data Set: {result.Key}, Total Size: {result.Value.Sum(d => d.Value)}");
  62.  
  63.             foreach (var value in result.Value)
  64.             {
  65.                 Console.WriteLine("$.{0}", value.Key);
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement