Advertisement
simonradev

Banana

Nov 3rd, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _03.Problem
  7. {
  8.     public class Startup
  9.     {
  10.         public static void Main()
  11.         {
  12.             DataSet dataSet = new DataSet();
  13.             dataSet.InitializeCache();
  14.  
  15.             string inputLine;
  16.             while (!(inputLine = Console.ReadLine()).Equals("thetinggoesskrra"))
  17.             {
  18.                 string[] args = SplitString(inputLine);
  19.  
  20.                 if (args.Length == 1)
  21.                 {
  22.                     string dataSetKey = args[0];
  23.  
  24.                     dataSet.AddDataSetKey(dataSetKey);
  25.                 }
  26.                 else
  27.                 {
  28.                     string dataKey = args[0];
  29.                     long dataSize = long.Parse(args[1]);
  30.                     string dataSetKey = args[2];
  31.  
  32.                     dataSet.AddDataSetValue(dataSetKey, dataKey, dataSize);
  33.                 }
  34.             }
  35.  
  36.             string result = dataSet.GetHighestSumDataSet();
  37.             Console.Write(result);
  38.         }
  39.  
  40.         private static string[] SplitString(string toSplit)
  41.         {
  42.             return toSplit.Split(new[] { " ", "->", "|" }, StringSplitOptions.RemoveEmptyEntries);
  43.         }
  44.     }
  45.  
  46.     public class DataSet
  47.     {
  48.         private Dictionary<string, Dictionary<string, long>> data;
  49.         private DataSet cache;
  50.  
  51.         public DataSet()
  52.         {
  53.             this.data = new Dictionary<string, Dictionary<string, long>>();
  54.         }
  55.  
  56.         public void InitializeCache()
  57.         {
  58.             this.cache = new DataSet();
  59.         }
  60.  
  61.         public string GetHighestSumDataSet()
  62.         {
  63.             if (this.data.Count == 0)
  64.             {
  65.                 return string.Empty;
  66.             }
  67.  
  68.             KeyValuePair<string, Dictionary<string, long>> resultSet = this.data
  69.                                                                            .OrderByDescending(kvp => kvp.Value.Values.Sum())
  70.                                                                            .FirstOrDefault();
  71.  
  72.             StringBuilder result = new StringBuilder();
  73.  
  74.             long totalSize = resultSet.Value.Values.Sum();
  75.             string header = $"Data Set: {resultSet.Key}, Total Size: {totalSize}";
  76.             result.AppendLine(header);
  77.  
  78.             foreach (KeyValuePair<string, long> kvp in resultSet.Value)
  79.             {
  80.                 result.AppendLine($"$.{kvp.Key}");
  81.             }
  82.  
  83.             return result.ToString();
  84.         }
  85.  
  86.         public void AddDataSetKey(string dataSetKey, bool useCache = true)
  87.         {
  88.             if (!this.data.ContainsKey(dataSetKey))
  89.             {
  90.                 this.data[dataSetKey] = new Dictionary<string, long>();
  91.  
  92.                 if (useCache)
  93.                 {
  94.                     this.ExtractFromCache(dataSetKey);
  95.                 }
  96.             }
  97.         }
  98.  
  99.         public void AddDataSetValue(string dataSetKey, string dataKey, long dataSize, bool useCache = true)
  100.         {
  101.             if (!this.data.ContainsKey(dataSetKey))
  102.             {
  103.                 if (useCache)
  104.                 {
  105.                     this.AddToCache(dataSetKey, dataKey, dataSize);
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 this.data[dataSetKey][dataKey] = dataSize;
  111.             }
  112.         }
  113.  
  114.         private void ExtractFromCache(string dataSetKey)
  115.         {
  116.             if (!this.cache.data.ContainsKey(dataSetKey))
  117.             {
  118.                 return;
  119.             }
  120.  
  121.             foreach (KeyValuePair<string, Dictionary<string, long>> cacheKvp in this.cache.data)
  122.             {
  123.                 string cacheDataSetKey = cacheKvp.Key;
  124.                 foreach (KeyValuePair<string, long> cacheDataKeyAndValue in cacheKvp.Value)
  125.                 {
  126.                     string cacheDataKey = cacheDataKeyAndValue.Key;
  127.                     long cacheDataSize = cacheDataKeyAndValue.Value;
  128.  
  129.                     this.AddDataSetValue(cacheDataSetKey, cacheDataKey, cacheDataSize, useCache: false);
  130.                 }
  131.             }
  132.            
  133.             this.cache.data.Remove(dataSetKey);
  134.         }
  135.  
  136.         private void AddToCache(string dataSetKey, string dataKey, long dataSize)
  137.         {
  138.             this.cache.AddDataSetKey(dataSetKey, useCache: false);
  139.             this.cache.AddDataSetValue(dataSetKey, dataKey, dataSize, useCache: false);
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement