Advertisement
grubcho

Forum topics - Nested dict

Jul 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 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 forum_topics
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, HashSet<string>> topics = new Dictionary<string, HashSet<string>>();
  14.             string inputline = Console.ReadLine();
  15.  
  16.             while (inputline != "filter")
  17.             {
  18.                 var inputData = inputline.Split(new[] { '-', '>', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  19.                 var currentKey = inputData[0];
  20.  
  21.                 if (!topics.ContainsKey(currentKey))
  22.                 {
  23.                     topics[currentKey] = new HashSet<string>();
  24.                 }
  25.                 for (int i = 1; i < inputData.Length; i++)
  26.                 {
  27.                     topics[currentKey].Add(inputData[i]);
  28.                 }
  29.                 inputline = Console.ReadLine();
  30.             }
  31.             var tagSequence = Console.ReadLine().Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  32.  
  33.             foreach (var entry in topics)
  34.             {
  35.                 bool containsTag = false;
  36.                 for (int i = 0; i < tagSequence.Count; i++)
  37.                 {
  38.                     if (entry.Value.Contains(tagSequence[i]))
  39.                     {
  40.                         containsTag = true;
  41.                     }
  42.                     else
  43.                     {
  44.                         containsTag = false;
  45.                         break;
  46.                     }
  47.                 }
  48.  
  49.                 var topic = entry.Key;
  50.                 var tags = entry.Value;
  51.  
  52.                 if (containsTag)
  53.                 {
  54.                     Console.WriteLine($"{topic} | #{string.Join(", #", tags)}");
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement