Guest User

Dictionary

a guest
Apr 12th, 2019
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace DEMO_Exam_Dictionary
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] wordAndExplain = Console.ReadLine().Split(" | ").ToArray();
  12.             string command = string.Empty;
  13.             var words = new Dictionary<string, List<string>>();
  14.  
  15.             if ((command = Console.ReadLine()) == "End") return;
  16.             for (int i = 0; i < wordAndExplain.Length; i++)
  17.             {
  18.                 string[] wordAndDefinition = wordAndExplain[i].Split(": ").ToArray();
  19.                 string wordName = wordAndExplain[0];
  20.                 string wordExplain = wordAndExplain[1];
  21.                 if (!words.ContainsKey(wordName))
  22.                 {
  23.                     words.Add(wordName, new List<string>());
  24.                 }
  25.                 else words[wordName].Add(wordExplain);
  26.             }
  27.             if ((command = Console.ReadLine()) == "List")
  28.             {
  29.                 foreach (var word in words.OrderByDescending(x => x))
  30.                 {
  31.                     Console.WriteLine($"{word.Key}:");
  32.  
  33.                     foreach (var item in words.OrderBy(x => x))
  34.                     {
  35.                         Console.WriteLine($" -{string.Join(" ", item)}");
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment