Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 03. Word Synonyms
- 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:
- • {word}
- • {synonym}
- If you get the same word twice, just add the new synonym to the list.
- Print the words in the following format:
- {word} - {synonym1, synonym2… synonymN}
- Examples
- Input Output
- 3
- cute cute - adorable, charming
- adorable smart - clever
- cute
- charming
- smart
- clever
- 2
- task task - problem, assignment
- problem
- task
- assignment
- Hints
- • Use a dictionary (string -> List<string>) to keep all of the synonyms.
- using System;
- using System.Collections.Generic;
- namespace _03WordSynonyms
- {
- class Program
- {
- static void Main(string[] args)
- {
- var count = int.Parse(Console.ReadLine());
- var synonymDictionary = new Dictionary<string, List<string>>();
- for (int i = 1; i <= count; i++)
- {
- var word = Console.ReadLine();
- var synonymWord = Console.ReadLine();
- if (synonymDictionary.ContainsKey(word) == false)
- {
- var listSynonym = new List<string>();
- synonymDictionary.Add(word, listSynonym);
- }
- synonymDictionary[word].Add(synonymWord);
- }
- foreach (var item in synonymDictionary)
- {
- Console.WriteLine($"{item.Key} - {string.Join(", ", item.Value)}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment