nikolapetkov824

ForceBook with List<string>

Dec 9th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace SoftUniExamResults
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Dictionary<string, List<string>> map =
  13.                 new Dictionary<string, List<string>>();
  14.  
  15.             while (true)
  16.             {
  17.                 string input = Console.ReadLine();
  18.  
  19.                 if (input=="Lumpawaroo")
  20.                 {
  21.                     break;
  22.                 }
  23.                 string[] checkedForceUsers = input.Split(new string[] { " | ", " -> " }, StringSplitOptions.RemoveEmptyEntries);
  24.  
  25.                 if (input.Contains("|"))
  26.                 {
  27.                     string forceSide = checkedForceUsers[0];
  28.                     string forceUser = checkedForceUsers[1];
  29.  
  30.                     if (!map.ContainsKey(forceSide))
  31.                     {
  32.                         map.Add(forceSide, new List<string>());
  33.                     }
  34.  
  35.                     if (!map.Values.Any(d => d.Contains(forceUser)))
  36.                     {
  37.                         map[forceSide].Add(forceUser);
  38.                     }
  39.                 }
  40.                 else
  41.                 {
  42.                     string forceUser = checkedForceUsers[0];
  43.                     string forceSide = checkedForceUsers[1];
  44.  
  45.                     foreach (var user in map)
  46.                     {
  47.                         user.Value.RemoveAll(x=>x == forceUser);
  48.                     }
  49.  
  50.                     if (!map.ContainsKey(forceSide))
  51.                     {
  52.                         map.Add(forceSide, new List<string>());
  53.                     }
  54.  
  55.                     if (!map.Values.Any(d => d.Contains(forceUser)))
  56.                     {
  57.                         map[forceSide].Add(forceUser);
  58.                         Console.WriteLine($"{forceUser} joins the {forceSide} side!");
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             foreach (var item in map
  64.                 .Where(x=>x.Value.Count > 0)
  65.                 .OrderByDescending(x=>x.Value.Count)
  66.                 .ThenBy(x=>x.Key))
  67.             {
  68.                 Console.WriteLine($"Side: {item.Key}, Members: {item.Value.Count}");
  69.  
  70.                 foreach (var user in item.Value.OrderBy(x=>x))
  71.                 {
  72.                     Console.WriteLine($"! {user}");
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment