Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05_Flatten_Dictionary
  6. {
  7. public class FlattenDictionary
  8. {
  9. public static void Main()
  10. {
  11. string line = Console.ReadLine();
  12.  
  13. var data = new Dictionary<string, Dictionary<string, string>>();
  14. var flatten = new Dictionary<string, Dictionary<string, string>>();
  15.  
  16. while (line != "end")
  17. {
  18. var tokens = line.Split().ToArray();
  19.  
  20. if (tokens[0] == "flatten")
  21. {
  22. string flattenType = tokens[1];
  23. if (data.ContainsKey(flattenType))
  24. {
  25. var dataToBeFlattened = data[flattenType];
  26. flatten.Add(flattenType, dataToBeFlattened);
  27. data.Remove(flattenType);
  28. }
  29. line = Console.ReadLine();
  30. continue;
  31. }
  32.  
  33. string type = tokens[0];
  34. string brand = tokens[1];
  35. string model = tokens[2];
  36.  
  37. if (!data.ContainsKey(type))
  38. {
  39. data.Add(type, new Dictionary<string, string>());
  40. }
  41.  
  42. if (data[type].ContainsKey(brand))
  43. {
  44. data[type][brand] = model;
  45. }
  46. else
  47. {
  48. data[type].Add(brand, model);
  49. }
  50.  
  51. line = Console.ReadLine();
  52. }
  53.  
  54. foreach (var kvp in data.OrderByDescending(x => x.Key.Length))
  55. {
  56. string type = kvp.Key;
  57. Console.WriteLine($"{type}");
  58. var brandsModels = kvp.Value;
  59. int cnt = 1;
  60. foreach (var brandModel in brandsModels.OrderBy(x => x.Key.Length))
  61. {
  62. string brand = brandModel.Key;
  63. string model = brandModel.Value;
  64.  
  65. Console.WriteLine($"{cnt}. {brand} - {model}");
  66. cnt++;
  67. }
  68. if (flatten.ContainsKey(type))
  69. {
  70. foreach (var flat in flatten)
  71. {
  72. string flattenType = flat.Key;
  73. if (flattenType == type)
  74. {
  75. var flattenBrandsModels = flat.Value;
  76. foreach (var flattenBrandModel in flattenBrandsModels)
  77. {
  78. string flattenBrand = flattenBrandModel.Key;
  79. string flattenModel = flattenBrandModel.Value;
  80. Console.WriteLine($"{cnt}. {flattenBrand}{flattenModel}");
  81. cnt++;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement