Advertisement
Guest User

02. Key-Key Value-Value

a guest
Jul 15th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 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.  
  7. namespace Key_Key_Value_Value
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, List<string>> kvpToPrint = new Dictionary<string, List<string>>();
  14.             string keyToFind = Console.ReadLine();
  15.             string valueToFind = Console.ReadLine();
  16.             int lines = int.Parse(Console.ReadLine());
  17.             string currentKey = string.Empty;
  18.             List<string> currentValues = new List<string>();
  19.  
  20.             for (int i = 0; i < lines; i++)
  21.             {
  22.                 string[] input = Console.ReadLine().Split(new string[] { " => " }
  23.                     , StringSplitOptions.RemoveEmptyEntries);
  24.                 currentKey = input[0];
  25.                 currentValues = input[1].Split(';').ToList();
  26.  
  27.                 if (currentKey.Contains(keyToFind))
  28.                 {
  29.                     if (!kvpToPrint.ContainsKey(currentKey))
  30.                     {
  31.                         kvpToPrint.Add(currentKey, new List<string>());
  32.                     }
  33.                 }
  34.                 for (int j = 0; j < currentValues.Count; j++)
  35.                 {
  36.                     if (currentValues[j].Contains(valueToFind))
  37.                     {
  38.                         kvpToPrint[currentKey].Add(currentValues[j]);
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             foreach (var kvPair in kvpToPrint)
  44.             {
  45.                 Console.WriteLine($"{kvPair.Key}:");
  46.                 foreach (var value in kvPair.Value)
  47.                 {
  48.                     Console.WriteLine($"-{value}");
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement