Advertisement
YavorGrancharov

Flatten_Dictionary(lambda)

Jul 20th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Flatten_Dictionary
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, Dictionary<string, string>> set =
  12.                 new Dictionary<string, Dictionary<string, string>>();
  13.  
  14.             string[] input = Console.ReadLine().Split(' ');
  15.             while (input[0] != "end")
  16.             {
  17.                 if (input.Length > 2)
  18.                 {
  19.                     string category = input[0];
  20.                     string innerKey = input[1];
  21.                     string innerValue = input[2];
  22.  
  23.                     if (!set.ContainsKey(category))
  24.                     {
  25.                         set[category] = new Dictionary<string, string>();
  26.                     }
  27.                     set[category][innerKey] = innerValue;
  28.                 }
  29.                 else
  30.                 {
  31.                     if (input[0] == "flatten")
  32.                     {
  33.                         string flat = input[1];
  34.  
  35.                         set[flat] = set[flat]
  36.                             .ToDictionary(p => p.Key + "" + p.Value, p => string.Empty);
  37.                     }
  38.                 }
  39.                 input = Console.ReadLine().Split(' ');
  40.             }
  41.  
  42.            
  43.             foreach (KeyValuePair<string, Dictionary<string, string>> kvp in set
  44.                 .OrderByDescending(p => p.Key.Length))
  45.             {
  46.                 string category = kvp.Key;
  47.  
  48.                 Console.WriteLine("{0}", category);
  49.  
  50.                 Dictionary<string, string> newSet = kvp.Value.Where(p => p.Value != string.Empty)
  51.                     .ToDictionary(p => p.Key, p => p.Value);              
  52.  
  53.                 Dictionary<string, string> newSet2 = kvp.Value.Where(p => p.Value == string.Empty)
  54.                     .ToDictionary(p => p.Key, p => p.Value);
  55.  
  56.                 int indexer = 1;
  57.                 foreach (KeyValuePair<string, string> kvpSet in newSet
  58.                     .OrderBy(p => p.Key.Length))
  59.                 {
  60.                     Console.WriteLine("{0}. {1} - {2}", indexer, kvpSet.Key, kvpSet.Value);
  61.                     indexer++;
  62.                 }
  63.  
  64.                 foreach (KeyValuePair<string, string> kvpSet in newSet2)
  65.                 {
  66.                     Console.WriteLine("{0}. {1}{2}", indexer, kvpSet.Key, kvpSet.Value);
  67.                     indexer++;
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement