SimeonTs

SUPyF2 Dict-Exercise - 09. ForceBook

Oct 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. """
  2. Dictionaries - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1737#8
  4.  
  5. SUPyF2 Dict-Exercise - 09. ForceBook (not included in final score)
  6.  
  7. Problem:
  8. The force users are struggling to remember which side are the different forceUsers from,
  9. because they switch them too often. So you are tasked to create a web application to manage their profiles.
  10. You should store an information for every unique forceUser, registered in the application.
  11. You will receive several input lines in one of the following formats:
  12. {forceSide} | {forceUser}
  13. {forceUser} -> {forceSide}
  14. The forceUser and forceSide are strings, containing any character.
  15. If you receive forceSide | forceUser, you should check if such forceUser already exists, and if not,
  16. add him/her to the corresponding side.
  17. If you receive a forceUser -> forceSide, you should check if there is such a forceUser already and if so,
  18. change his/her side. If there is no such forceUser, add him/her to the corresponding forceSide,
  19. treating the command as a new registered forceUser.
  20. Then you should print on the console: "{forceUser} joins the {forceSide} side!"
  21. You should end your program when you receive the command "Lumpawaroo".
  22. At that point you should print each force side, ordered descending by forceUsers count,
  23. than ordered by name. For each side print the forceUsers, ordered by name.
  24. In case there are no forceUsers in a side, you shouldn`t print the side information.
  25. Input / Constraints
  26. • The input comes in the form of commands in one of the formats specified above.
  27. • The input ends, when you receive the command "Lumpawaroo".
  28. Output
  29. • As output for each forceSide, ordered descending by forceUsers count, then by name,
  30. you must print all the forceUsers, ordered by name alphabetically.
  31. • The output format is:
  32. Side: {forceSide}, Members: {forceUsers.Count}
  33. ! {forceUser}
  34. ! {forceUser}
  35. ! {forceUser}
  36. • In case there are NO forceUsers, don`t print this side.
  37. Examples:
  38. Input:
  39. Light | Gosho
  40. Dark | Pesho
  41. Lumpawaroo
  42.  
  43. Output:
  44. Side: Dark, Members: 1
  45. ! Pesho
  46. Side: Light, Members: 1
  47. ! Gosho
  48.  
  49. Comments:
  50. We register Gosho in the Light side and Pesho in the Dark side.
  51. After receiving "Lumpawaroo" we print both sides, ordered by membersCount and then by name.
  52.  
  53. Input;
  54. Lighter | Royal
  55. Darker | DCay
  56. Ivan Ivanov -> Lighter
  57. DCay -> Lighter
  58. Lumpawaroo
  59.  
  60. Output:
  61. Ivan Ivanov joins the Lighter side!
  62. DCay joins the Lighter side!
  63. Side: Lighter, Members: 3
  64. ! DCay
  65. ! Ivan Ivanov
  66. ! Royal
  67.  
  68. Comments:
  69. Although Ivan Ivanov doesn`t have profile, we register him and add him to the Lighter side.
  70. We remove DCay from Darker side and add him to Lighter side.
  71. We print only Lighter side because Darker side has no members.
  72. """
  73. sides = {}
  74.  
  75. while True:
  76.     command = input()
  77.  
  78.     if command == "Lumpawaroo":
  79.         break
  80.  
  81.     tokens = command.split(" | ")
  82.  
  83.     if len(tokens) == 2:
  84.         side, user = tokens
  85.         is_contained = False
  86.         for key, value in sides.items():
  87.             if user in value:
  88.                 is_contained = True
  89.         if not is_contained:
  90.             if side not in sides:
  91.                 sides[side] = []
  92.             sides[side].append(user)
  93.  
  94.     else:
  95.         tokens = command.split(" -> ")
  96.         user, side = tokens
  97.  
  98.         for key, value in sides.items():
  99.             if user in value:
  100.                 sides[key].remove(user)
  101.  
  102.         if side not in sides:
  103.             sides[side] = []
  104.         sides[side].append(user)
  105.         print(f"{user} joins the {side} side!")
  106.  
  107. for side, users in sorted(sides.items(), key=lambda x: (-len(x[1]), x[0])):
  108.     if len(users) >= 1:
  109.         print(f"Side: {side}, Members: {len(users)}")
  110.         for user in sorted(users):
  111.             print(f"! {user}")
Add Comment
Please, Sign In to add comment