Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _1._Dictionary
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.  
  12.             //First command start
  13.             var firstCommand = Console.ReadLine()
  14.                 .Split(" | ")
  15.                 .ToList();
  16.  
  17.             var result = new List<string>();
  18.  
  19.             for (int i = 0; i < firstCommand.Count; i++)
  20.             {
  21.                 string[] check = firstCommand[i].Split(": ");
  22.                 result.Add(check[0]);
  23.                 result.Add(check[1]);
  24.             }
  25.  
  26.             var dict = new SortedDictionary<string, List<string>>();
  27.  
  28.             for (int i = 0; i < result.Count; i += 2)
  29.             {
  30.                 if (dict.ContainsKey(result[i]))
  31.                 {
  32.                     dict[result[i]].Add(result[i + 1]);
  33.                 }
  34.                 else
  35.                 {
  36.                     dict.Add(result[i], new List<string> { result[i + 1] });
  37.                 }
  38.             }
  39.  
  40.             //firstCommand end
  41.  
  42.             //secondCommand start
  43.  
  44.             var secondCommand = Console.ReadLine()
  45.                 .Split(" | ")
  46.                 .ToList();
  47.  
  48.             for (int i = 0; i < secondCommand.Count; i++)
  49.             {
  50.                 if (dict.ContainsKey(secondCommand[i]))
  51.                 {
  52.                     Console.WriteLine(secondCommand[i]);
  53.  
  54.                     foreach (var value in dict[secondCommand[i]])
  55.                     {
  56.                         Console.WriteLine($" -{value}");
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             //secondCommand end
  62.  
  63.             while (true)
  64.             {
  65.                 var thirdCommand = Console.ReadLine().ToLower();
  66.  
  67.                 if (thirdCommand == "end")
  68.                 {
  69.                     break;
  70.                 }
  71.                 else if (thirdCommand == "list")
  72.                 {
  73.                     foreach (var item in dict)
  74.                     {
  75.                         Console.Write(item.Key + " ");
  76.                     }
  77.  
  78.                     Console.WriteLine();
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement