simeon3000

dict-ref

Oct 27th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class test
  5. {
  6.     static void Main()
  7.     {
  8.         string[] line = Console.ReadLine().Split();
  9.         var dict = new Dictionary<string, int>();
  10.  
  11.         while (line[0] != "end")
  12.         {
  13.             string firstName = line[0];
  14.             string secondPart = line[2];
  15.  
  16.             bool isNumber = int.TryParse(secondPart, out int num);
  17.  
  18.             if (isNumber)
  19.             {
  20.                 dict[firstName] = num;
  21.             }
  22.             else
  23.             {
  24.                 if (dict.ContainsKey(secondPart))
  25.                 {
  26.                     dict[firstName] = dict[secondPart];
  27.                 }
  28.             }
  29.  
  30.             line = Console.ReadLine().Split();
  31.         }
  32.  
  33.         foreach (var item in dict)
  34.         {
  35.             Console.WriteLine($"{item.Key} === {item.Value}");
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment