JulianJulianov

14.AsociativeArrays-*ForceBook

Apr 12th, 2020
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. 14. *ForceBook
  2. The force users are struggling to remember which side are the different forceUsers from, because they switch them too often. So you are tasked to create a web application to manage their profiles. You should store an information for every unique forceUser, registered in the application.
  3. You will receive several input lines in one of the following formats:
  4. {forceSide} | {forceUser}
  5. {forceUser} -> {forceSide}
  6. The forceUser and forceSide are strings, containing any character.
  7. If you receive forceSide | forceUser, you should check if such forceUser already exists, and if not, add him/her to the corresponding side.
  8. If you receive a forceUser -> forceSide, you should check if there is such a forceUser already and if so, change his/her side. If there is no such forceUser, add him/her to the corresponding forceSide, treating the command as a new registered forceUser.
  9. Then you should print on the console: "{forceUser} joins the {forceSide} side!"
  10. You should end your program when you receive the command "Lumpawaroo". At that point you should print each force side, ordered descending by forceUsers count, than ordered by name. For each side print the forceUsers, ordered by name.
  11. In case there are no forceUsers in a side, you shouldn`t print the side information.
  12. Input / Constraints
  13. • The input comes in the form of commands in one of the formats specified above.
  14. • The input ends, when you receive the command "Lumpawaroo".
  15. Output
  16. As output for each forceSide, ordered descending by forceUsers count, then by name,  you must print all the forceUsers, ordered by name alphabetically.
  17. • The output format is:
  18. Side: {forceSide}, Members: {forceUsers.Count}
  19. ! {forceUser}
  20. ! {forceUser}
  21. ! {forceUser}
  22. In case there are NO forceUsers, don`t print this side.
  23. Examples
  24. Input                                     Output                                 Comments
  25. Light | Gosho                             Side: Dark, Members: 1                 We register Gosho in the Light side and Pesho
  26. Dark | Pesho                              ! Pesho                                in the Dark side. After receiving "Lumpawaroo" we Lumpawaroo                                 Side: Light, Members: 1                print  both sides, ordered by membersCount
  27.                                           ! Gosho                                and then by name.
  28.  
  29.      
  30. Lighter | Royal                           Ivan Ivanov joins the Lighter side!    Although Ivan Ivanov doesn`t have profile,
  31. Darker | DCay                             DCay joins the Lighter side!           we register him and add him to the Lighter side.
  32. Ivan Ivanov -> Lighter                    Side: Lighter, Members: 3              We remove DCay from Darker side and add him
  33. DCay -> Lighter                           ! DCay                                 to Lighter side. We print only Lighter side
  34. Lumpawaroo                                ! Ivan Ivanov                           because Darker side has no members.
  35.                                           ! Royal
  36.  
  37.  
  38.  
  39. using System;
  40. using System.Collections.Generic;
  41. using System.Linq;
  42.  
  43. namespace _09ForceBook
  44. {
  45.     class Program
  46.     {
  47.         static void Main(string[] args)
  48.         {
  49.             var forceUsers = new Dictionary<string, string>();
  50.  
  51.             var command = string.Empty;
  52.  
  53.             while ((command = Console.ReadLine()) != "Lumpawaroo")
  54.             {
  55.                 var test = command.Split();
  56.                 if (test.Contains("|"))
  57.                 {
  58.                     var commandArr = command.Split(" | ");
  59.                     var forceSide = commandArr[0];
  60.                     var forceUser = commandArr[1];
  61.  
  62.                     if (!forceUsers.ContainsKey(forceUser))
  63.                         forceUsers.Add(forceUser, forceSide);
  64.                 }
  65.                 else if(test.Contains("->"))
  66.                 {
  67.                     var commandArr = command.Split(" -> ");
  68.                     var forceUser = commandArr[0];
  69.                     var forceSide = commandArr[1];
  70.  
  71.                     if (forceUsers.ContainsKey(forceUser))
  72.                         forceUsers[forceUser] = forceSide;
  73.                     else
  74.                         forceUsers.Add(forceUser,forceSide);
  75.  
  76.                     Console.WriteLine($"{forceUser} joins the {forceSide} side!");
  77.                 }
  78.             }
  79.  
  80.             foreach (var users in forceUsers
  81.                 .GroupBy(x => x.Value)
  82.                 .OrderByDescending(x => x.Count())
  83.                 .ThenBy(x => x.Key))
  84.             {
  85.                 Console.WriteLine($"Side: {users.Key}, Members: {users.Count()}");
  86.  
  87.                 foreach (var item in users.OrderBy(x => x.Key))
  88.                 {
  89.                     Console.WriteLine($"! {item.Key}");
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment