Advertisement
grubcho

Social posts - Nested dict

Jul 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 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 Social_posts
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var socialMedia = new Dictionary<string, Dictionary<string, List<string>>>();
  14.             var input = Console.ReadLine();
  15.  
  16.             while (input != "drop the media")
  17.             {
  18.                 var tokens = input.Split(' ').ToArray();
  19.                 var command = tokens[0];
  20.                 var postName = tokens[1];
  21.  
  22.                 if (!socialMedia.ContainsKey(postName))
  23.                 {
  24.                     socialMedia[postName] = new Dictionary<string, List<string>>();
  25.                 }
  26.                 if (command == "like")
  27.                 {
  28.                     if (!socialMedia[postName].ContainsKey("Like"))
  29.                     {
  30.                         socialMedia[postName]["Like"] = new List<string>();
  31.                     }
  32.  
  33.                     socialMedia[postName]["Like"].Add(command);
  34.                 }
  35.                 else if (command == "dislike")
  36.                 {
  37.                     if (!socialMedia[postName].ContainsKey("Dislike"))
  38.                     {
  39.                         socialMedia[postName]["Dislike"] = new List<string>();
  40.                     }
  41.  
  42.                     socialMedia[postName]["Dislike"].Add(command);
  43.                 }
  44.                 else if (command == "comment")
  45.                 {
  46.                     var writer = tokens[2];
  47.                     int length = command.Length + postName.Length + writer.Length + 3;
  48.                     var comment = input.Substring(length);
  49.                     if (!socialMedia[postName].ContainsKey(writer))
  50.                     {
  51.                         socialMedia[postName][writer] = new List<string>();
  52.                     }
  53.  
  54.                     socialMedia[postName][writer].Add(comment);
  55.                 }
  56.  
  57.                 input = Console.ReadLine();
  58.             }
  59.  
  60.             foreach (var post in socialMedia)
  61.             {
  62.                 var likes = 0;
  63.                 var dislikes = 0;
  64.                 var writers = post.Value;
  65.                 foreach (var item in post.Value)
  66.                 {
  67.                     if (item.Key == "Like")
  68.                     {
  69.                         likes = item.Value.Count;
  70.                     }
  71.                     else if (item.Key == "Dislike")
  72.                     {
  73.                         dislikes = item.Value.Count;
  74.                     }
  75.                 }
  76.                 Console.WriteLine($"Post: {post.Key} | Likes: {likes} | Dislikes: {dislikes}");
  77.                 Console.WriteLine("Comments:");
  78.  
  79.                 bool noComment = true;
  80.                 foreach (var comment in writers)
  81.                 {
  82.                     if (comment.Key != "Like" && comment.Key != "Dislike")
  83.                     {
  84.                         noComment = false;
  85.                         foreach (var com in comment.Value)
  86.                         {
  87.                             Console.WriteLine($"*  {comment.Key}: {com}");
  88.                         }
  89.                     }
  90.                 }
  91.  
  92.                 if (noComment)
  93.                 {
  94.                     Console.WriteLine("None");
  95.                 }
  96.             }
  97.         }    
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement