Aliendreamer

dict ref advanced

Jul 11th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 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 _05NestedDictionariesExcercisesDic_RefAdvanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, List<int>> data = new Dictionary<string, List<int>>();
  14.  
  15.             string input = Console.ReadLine();
  16.  
  17.             while(input!="end")
  18.             {
  19.  
  20.                 string []inputTokens = input.Split(new string[] { "->" }
  21.                 , StringSplitOptions.RemoveEmptyEntries).ToArray();
  22.  
  23.                 string name = inputTokens[0];
  24.  
  25.                 if(IsName(inputTokens[1]))
  26.                 {
  27.                    
  28.                     string otherName = inputTokens[1];
  29.  
  30.                     if(data.ContainsKey(otherName))
  31.                     {
  32.                         List<int> otherNumbers = data[otherName];
  33.  
  34.  
  35.                         if(!data.ContainsKey(name))
  36.                         {
  37.                             data.Add(name, new List<int>());
  38.                         }
  39.  
  40.                         data[name].Clear();
  41.                         data[name].AddRange(otherNumbers);
  42.                     }
  43.  
  44.  
  45.  
  46.                 }
  47.                 else
  48.                 {
  49.                     int[] numbers = inputTokens[1].Split(new string[] { ", " }
  50.                     , StringSplitOptions.RemoveEmptyEntries)
  51.                         .Select(int.Parse).ToArray();
  52.  
  53.                     if (!data.ContainsKey(name))
  54.                     {
  55.                         data.Add(name, new List<int>());
  56.  
  57.                     }
  58.                     data[name].AddRange(numbers);
  59.                 }
  60.  
  61.             input = Console.ReadLine();
  62.  
  63.             }
  64.             foreach (KeyValuePair<string,List<int>> item in data)
  65.             {
  66.                 string name = item.Key;
  67.                 List<int> num = item.Value;
  68.                 Console.WriteLine($"{name}=== {string.Join(", ",num)}");
  69.             }
  70.  
  71.  
  72.         }
  73.         static bool IsName(string input)
  74.         {
  75.             foreach (char ch in input)
  76.             {
  77.  
  78.                 if (char.IsLetter(ch))
  79.                 {
  80.                     return true;
  81.                 }
  82.  
  83.             }
  84.             return false;
  85.         }
  86.            
  87.     }
  88.    
  89. }
Advertisement
Add Comment
Please, Sign In to add comment