Advertisement
Guest User

Untitled

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