Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //You have been tasked to create a referenced dictionary, or in other words a dictionary, which knows how to reference itself.
- //You will be given several input lines, in one of the following formats:
- //β’ {name} = {value}
- //β’ {name} = {secondName}
- //The names will always be strings, and the values will always be integers.
- //In case you are given a name and a value, you must store the given name and its value. If the name already EXISTS, you must CHANGE //its value with the given one.
- //In case you are given a name and a second name, you must store the given name with the same value as the value of the second name. If //the given second name DOES NOT exist, you must IGNORE that input.
- //When you receive the command βendβ, you must print all entries with their value, by order of input, in the following format:
- //{entry} === {value}
- namespace dict_ref
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = Console.ReadLine();
- Dictionary<string, int> dictionary = new Dictionary<string, int>();
- while (input != "end")
- {
- string[] inputData = input.Split(' ').ToArray();
- string name = inputData[0];
- string value = inputData[inputData.Length - 1];
- bool isNumber = value.All(char.IsDigit);
- if (isNumber)
- {
- dictionary[name] = int.Parse(value);
- }
- else
- {
- if (dictionary.ContainsKey(value))
- {
- var temp = dictionary[value];
- dictionary[name] = temp;
- }
- }
- input = Console.ReadLine();
- }
- foreach (var entry in dictionary)
- {
- Console.WriteLine($"{entry.Key} === {entry.Value}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement