Advertisement
valkata

05.Dict.Ref-Advance от Nested Dictionaries Exercis

Jul 12th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 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 dictRef
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, List<string>> people = new Dictionary<string, List<string>>();
  14.  
  15.             string input = Console.ReadLine();
  16.             while (!(input == "end"))
  17.             {
  18.                 string[] inputTokkens = input.Split(new string[] { " -> ", ", " }, StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 string name = inputTokkens[0];
  21.                 if (!(people.ContainsKey(name)))
  22.                 {
  23.                     people.Add(name, new List<string>());
  24.                     for (int i = 1; i < inputTokkens.Length; i++)
  25.                     {
  26.                         int number = 0;
  27.                         if (int.TryParse(inputTokkens[i], out number))
  28.                         {
  29.                             people[name].Add(inputTokkens[i]);
  30.                         }
  31.                         else
  32.                         {
  33.                             if (people.ContainsKey(inputTokkens[i]))
  34.                             {
  35.                                 people[name] = people[inputTokkens[i]];
  36.                             }
  37.                             else
  38.                             {
  39.                                 people.Remove(name);
  40.                             }
  41.                         }
  42.                     }
  43.                 }
  44.                 else
  45.                 {
  46.  
  47.                     for (int i = 1; i < inputTokkens.Length; i++)
  48.                     {
  49.                         int number = 0;
  50.                         if (int.TryParse(inputTokkens[i], out number))
  51.                         {
  52.                             people[name].Add(inputTokkens[i]);
  53.                         }
  54.                         else
  55.                         {
  56.                             if (people.ContainsKey(inputTokkens[i]))
  57.                             {
  58.                                 people[name] = people[inputTokkens[i]];
  59.                             }
  60.                             else
  61.                             {
  62.                                 people.Remove(name);
  63.                             }
  64.                         }
  65.                     }
  66.                 }
  67.  
  68.                 input = Console.ReadLine();
  69.             }
  70.  
  71.             foreach (KeyValuePair<string, List<string>> person in people)
  72.             {
  73.                 string name = person.Key;
  74.                 List<string> numbers = person.Value;
  75.  
  76.                 Console.WriteLine($"{name} === {string.Join(", ", numbers)}");
  77.  
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement