JulianJulianov

03.AssociativeArray - Word Synonyms

Apr 12th, 2020
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. 03. Word Synonyms
  2. Write a program, which keeps a dictionary with synonyms. The key of the dictionary will be the word. The value will be a list of all the synonyms of that word. You will be given a number n – the count of the words. After each word, you will be given a synonym, so the count of lines you have to read from the console is 2 * n. You will be receiving a word and a synonym each on a separate line like this:
  3. {word}
  4. {synonym}
  5. If you get the same word twice, just add the new synonym to the list.
  6. Print the words in the following format:
  7. {word} - {synonym1, synonym2… synonymN}
  8. Examples
  9. Input                              Output
  10. 3
  11. cute                               cute - adorable, charming
  12. adorable                           smart - clever
  13. cute
  14. charming
  15. smart
  16. clever 
  17.  
  18. 2
  19. task                               task - problem, assignment
  20. problem
  21. task
  22. assignment 
  23. Hints
  24. • Use a dictionary (string -> List<string>) to keep all of the synonyms.
  25.  
  26. using System;
  27. using System.Collections.Generic;
  28.  
  29. namespace _03WordSynonyms
  30. {
  31.     class Program
  32.     {
  33.         static void Main(string[] args)
  34.         {
  35.             var count = int.Parse(Console.ReadLine());
  36.             var synonymDictionary = new Dictionary<string, List<string>>();
  37.  
  38.             for (int i = 1; i <= count; i++)
  39.             {
  40.                 var word = Console.ReadLine();
  41.                 var synonymWord = Console.ReadLine();
  42.  
  43.                 if (synonymDictionary.ContainsKey(word) == false)
  44.                 {
  45.                     var listSynonym = new List<string>();                        
  46.                     synonymDictionary.Add(word, listSynonym);
  47.                 }
  48.                 synonymDictionary[word].Add(synonymWord);
  49.             }
  50.             foreach (var item in synonymDictionary)
  51.             {
  52.                 Console.WriteLine($"{item.Key} - {string.Join(", ", item.Value)}");
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment