Advertisement
Guest User

02. Dict-Ref-Advanced

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