Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _05_Flatten_Dictionary
- {
- public class FlattenDictionary
- {
- public static void Main()
- {
- string line = Console.ReadLine();
- var data = new Dictionary<string, Dictionary<string, string>>();
- var flatten = new Dictionary<string, Dictionary<string, string>>();
- while (line != "end")
- {
- var tokens = line.Split().ToArray();
- if (tokens[0] == "flatten")
- {
- string flattenType = tokens[1];
- if (data.ContainsKey(flattenType))
- {
- var dataToBeFlattened = data[flattenType];
- flatten.Add(flattenType, dataToBeFlattened);
- data.Remove(flattenType);
- }
- line = Console.ReadLine();
- continue;
- }
- string type = tokens[0];
- string brand = tokens[1];
- string model = tokens[2];
- if (!data.ContainsKey(type))
- {
- data.Add(type, new Dictionary<string, string>());
- }
- if (data[type].ContainsKey(brand))
- {
- data[type][brand] = model;
- }
- else
- {
- data[type].Add(brand, model);
- }
- line = Console.ReadLine();
- }
- foreach (var kvp in data.OrderByDescending(x => x.Key.Length))
- {
- string type = kvp.Key;
- Console.WriteLine($"{type}");
- var brandsModels = kvp.Value;
- int cnt = 1;
- foreach (var brandModel in brandsModels.OrderBy(x => x.Key.Length))
- {
- string brand = brandModel.Key;
- string model = brandModel.Value;
- Console.WriteLine($"{cnt}. {brand} - {model}");
- cnt++;
- }
- if (flatten.ContainsKey(type))
- {
- foreach (var flat in flatten)
- {
- string flattenType = flat.Key;
- if (flattenType == type)
- {
- var flattenBrandsModels = flat.Value;
- foreach (var flattenBrandModel in flattenBrandsModels)
- {
- string flattenBrand = flattenBrandModel.Key;
- string flattenModel = flattenBrandModel.Value;
- Console.WriteLine($"{cnt}. {flattenBrand}{flattenModel}");
- cnt++;
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement