desislava777

Dict-Ref-Advanced

Jul 10th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 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 ConsoleApplication18
  8. {
  9.     class Program
  10.     {
  11.         static bool IsName(string input)
  12.         {
  13.             foreach(char ch in input)
  14.             {
  15.                 if (char.IsLetter(ch))
  16.                 {
  17.                     return true;
  18.                 }
  19.             }
  20.             return false;
  21.         }
  22.         static void Main(string[] args)
  23.         {
  24.             var data = new Dictionary<string, List<int>>();
  25.             string input = Console.ReadLine();
  26.             while(input!="end")
  27.             {
  28.                 string[] inputTokens = input.Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
  29.                 string name = inputTokens[0];
  30.                 if (IsName(inputTokens[1]))
  31.                 {
  32.                     string otherName = inputTokens[1];
  33.                     if (data.ContainsKey(otherName))
  34.                     {
  35.                         List<int> otherNumbers = data[otherName];
  36.                         data[name].Clear();
  37.                         data[name].AddRange(otherNumbers);
  38.                     }              
  39.                 }
  40.                 else
  41.                 {
  42.                     int[] numbers = inputTokens[1]
  43.                    .Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)
  44.                    .Select(int.Parse).ToArray();
  45.                     if (!data.ContainsKey(name))
  46.                     {
  47.                         data.Add(name, new List<int>());
  48.                     }
  49.                     data[name].AddRange(numbers);
  50.                 }
  51.                 input = Console.ReadLine();
  52.             }
  53.             foreach(var record in data)
  54.             {
  55.                 string name = record.Key;
  56.                 List<int> numbers = record.Value;
  57.                 Console.WriteLine("{0} === {1}", name, string.Join(", ", numbers));
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment