Advertisement
grubcho

Dict - ref- bool isNumber = value.All(char.IsDigit);

Jul 10th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You have been tasked to create a referenced dictionary, or in other words a dictionary, which knows how to reference itself.
  7. //You will be given several input lines, in one of the following formats:
  8. //β€’   {name} = {value}
  9. //β€’   {name} = {secondName}
  10. //The names will always be strings, and the values will always be integers.
  11. //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.
  12. //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.
  13. //When you receive the command β€œend”, you must print all entries with their value, by order of input, in the following format:
  14. //{entry} === {value}
  15.  
  16. namespace dict_ref
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             var input = Console.ReadLine();
  23.             Dictionary<string, int> dictionary = new Dictionary<string, int>();
  24.  
  25.             while (input != "end")
  26.             {
  27.                 string[] inputData = input.Split(' ').ToArray();
  28.                 string name = inputData[0];
  29.                 string value = inputData[inputData.Length - 1];
  30.                
  31.                 bool isNumber = value.All(char.IsDigit);
  32.                 if (isNumber)
  33.                 {
  34.                     dictionary[name] = int.Parse(value);
  35.                 }
  36.                 else
  37.                 {
  38.                     if (dictionary.ContainsKey(value))
  39.                     {
  40.                         var temp = dictionary[value];
  41.                         dictionary[name] = temp;
  42.                     }
  43.                 }
  44.  
  45.                 input = Console.ReadLine();
  46.             }
  47.             foreach (var entry in dictionary)
  48.             {
  49.                 Console.WriteLine($"{entry.Key} === {entry.Value}");
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement