Advertisement
JulianJulianov

Dictionary

Jun 28th, 2020
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. Dictionary
  2. Gencho is in the library, looking for words and their definitions to fill in his dictionary in order to be able to crack an encrypted code and find an ancient relic.
  3. Your task is to take every word and insert it into the dictionary with its definition. A word may have one or more definitions. You will receive all the words and definitions, separated by " | ", and each word and its definition will be separated by ": ".
  4. After this you will have to check the words you have in the dictionary. Now you will receive only words, again separated by " | ". For each word you get you will have to print it and all of its definitions, ordered by length of the definition in descending order (if it exists in the dictionary) in the following format:
  5. "{word}:"
  6. " –{definition1}"
  7. " –{definition2}"
  8. " –{definition3}"
  9. . . .
  10. In the end, you will receive one more command, which will be either "End" or "List". If the command is "End", you should break the program. If the command is "List", you should print all of the words, ordered alphabetically, separated by space (" ").
  11. Input
  12. Three strings. The first one will have pairs of words and descriptions, separated by " | " and each word separated from its description by ": ". The second string will have only words, separated by " | ". The third string will be a command – either "End" or "List".
  13. Output
  14. For each word that is called you have to print it with all of its definitions ordered by their length (descending). In the end you have to print all the words, ordered alphabetically, separated by a single space if you have the command "List".  For all of the words you have to print them in the format:
  15. "{word}:"
  16. " –{definition1}"
  17. " –{definition2}"
  18. " –{definition3}"
  19. . . .
  20. Examples
  21. Input
  22. programmer: an animal, which turns coffee into code | developer: a magician
  23. Pesho | Gosho
  24. List
  25. Output
  26. developer programmer
  27.  
  28. Input
  29. tackle: the equipment required for a task or sport | code: write code for a computer program | bit: a small piece, part, or quantity of something | tackle: make determined efforts to deal with a problem | bit: a short time or distance
  30. bit | code | tackle
  31. End
  32. Output
  33. bit
  34.  -a small piece, part, or quantity of something
  35.  -a short time or distance
  36. code
  37.  -write code for a computer program
  38. tackle
  39.  -make determined efforts to deal with a problem
  40.  -the equipment required for a task or sport
  41.  
  42. using System;
  43. using System.Collections.Generic;
  44. using System.Linq;
  45.  
  46. public class Program
  47. {
  48.     public static void Main()
  49.     {
  50.             var firstString = Console.ReadLine().Split(" | ");
  51.             var listDictionary = new SortedDictionary<string, List<string>>();
  52.             foreach (var item in firstString)
  53.             {
  54.                 var wordDefinition = item.Split(": ");
  55.                 if (!listDictionary.ContainsKey(wordDefinition[0]))
  56.                 {
  57.                     listDictionary.Add(wordDefinition[0], new List<string>(){$" -{wordDefinition[1]}"});
  58.                 }
  59.                 else
  60.                 {
  61.                     listDictionary[wordDefinition[0]].Add($" -{wordDefinition[1]}");
  62.                 }
  63.             }
  64.             var secondCheckWordString = Console.ReadLine().Split(" | ");
  65.             foreach (var word in secondCheckWordString)
  66.             {
  67.                 if (listDictionary.ContainsKey(word))
  68.                 {
  69.                     foreach (var item in listDictionary.Where(x => x.Key == word))//Така се посочва да използваме само една(word) от
  70.                                                                                   //всички ключови думи в речника,
  71.                     {                                                             //защото иначе ще имаме няколко повтарящи се изхода!
  72.                         Console.WriteLine(item.Key);
  73.                         Console.WriteLine(string.Join("\n", item.Value.OrderByDescending(x => x.Length))); //Моят начин!
  74.                       //foreach (var definition in item.Value.OrderByDescending(x => x.Length))//Друг начин.
  75.                       //{
  76.                          //Console.WriteLine(definition);
  77.                       //}
  78.                     }
  79.                 }
  80.             }
  81.             var thirdString = Console.ReadLine();
  82.             if (thirdString == "List")
  83.             {
  84.                 Console.WriteLine(string.Join(" ", listDictionary.Select(x => x.Key)));//Друг начин.
  85.                 //var counter = 0;                              //Избери(Select) само ключа(думата) от речника(listDictionary)!
  86.                 //foreach (var item in listDictionary)
  87.                 //{
  88.                 //    counter++;
  89.                 //    if (listDictionary.Count != counter)
  90.                 //    {                                         //Моят начин.
  91.                 //        Console.Write($"{item.Key} ");
  92.                 //    }
  93.                 //    else
  94.                 //    {
  95.                 //        Console.WriteLine($"{item.Key}");
  96.                 //    }
  97.                 //}
  98.             }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement