2.Dict Ref from Dictionaries - Exercises???
Ако може някой да ми оправи кода, защото аз блокирах.
УСЛОВИЕ:
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:
EXAMPLES:
Input |
Output |
Cash = 500 Johny = 5 Cash = 200 Johnny = 200 Car = 150 Plane = 2000000 end |
Cash === 200 Johny === 5 Johnny === 200 Car === 150 Plane === 2000000 |
Entry1 = 10000 Entry2 = 12345 Entry3 = 10101 Entry4 = Entry1 Entry2 = Entry3 Entry3 = Entry4 end |
Entry1 === 10000 Entry2 === 10101 Entry3 === 10000 Entry4 === 10000
|
РЕШЕНИЕ:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2.Dict_Ref
{
class Program
{
static void Main(string[] args)
{
var line = Console.ReadLine().Split(' ').ToArray();
var dict = new Dictionary<string, string>();
while (line[0] != "end")
{
var name = line[0];
var num = line[2];
if (dict.ContainsKey(name))
{
dict[name] = num;
}
if (!dict.ContainsKey(name))
{
dict.Add(name, num);
}
line = Console.ReadLine().Split(' ').ToArray();
}
foreach (var item in dict)
{
Console.WriteLine($"{item.Key} === {item.Value}");
}
}
}
}