Advertisement
grubcho

Dict - Ref - Nested dict

Jul 17th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Dict_Ref_Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, List<int>> dictionary = new Dictionary<string, List<int>>();
  14.             string inputLine = Console.ReadLine();
  15.             while (inputLine != "end")
  16.             {
  17.                 string[] inputData = inputLine.Split(new[] { ' ', '-', '>', ',' }, StringSplitOptions.RemoveEmptyEntries);
  18.                 string currentKey = inputData[0];
  19.                 var firstValue = -1;
  20.                 if (int.TryParse(inputData[1], out firstValue))
  21.                 {
  22.                     if (!dictionary.ContainsKey(currentKey))
  23.                     {
  24.                         dictionary[currentKey] = new List<int>();
  25.                     }
  26.                     for (int i = 1; i < inputData.Length; i++)
  27.                     {
  28.                         dictionary[currentKey].Add(int.Parse(inputData[i]));
  29.                     }
  30.                 }
  31.                 else
  32.                 {
  33.                     string otherKey = inputData[1];
  34.                     if (dictionary.ContainsKey(otherKey))
  35.                     {
  36.                         dictionary[currentKey] = new List<int>(dictionary[otherKey]);
  37.                     }
  38.                 }
  39.                 inputLine = Console.ReadLine();
  40.             }//end while
  41.             foreach (var entry in dictionary)
  42.             {
  43.                 Console.WriteLine("{0} === {1}", entry.Key, string.Join(", ", entry.Value));
  44.             }
  45.  
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement