Advertisement
GeorgiLukanov87

11. Force Book_Dictionaries - Exercise 100/100 Functions

Jul 5th, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. # 11. Force Book 100/100
  2. # Dictionaries - Exercise
  3. # https://judge.softuni.org/Contests/Compete/Index/1737#10
  4.  
  5.  
  6. def force_user_exists(force_book_dict: dict, user: str):
  7.     for user_list in force_book_dict.values():
  8.         if user in user_list:
  9.             return True
  10.     return False
  11.  
  12.  
  13. def remove_user_from_side(force_book_dict: dict, user: str):
  14.     for (side, users) in force_book_dict.items():
  15.         if user in users:
  16.             force_book_dict[side].remove(user)
  17.  
  18.  
  19. def initial_join_force_side(force_book_dict: dict, side: str, user: str):
  20.     if side not in force_book_dict.keys() and not force_user_exists(force_book_dict, user):
  21.         force_book_dict[side] = []
  22.         force_book_dict[side].append(user)
  23.  
  24.     elif not force_user_exists(force_book_dict, user):
  25.         force_book_dict[side].append(user)
  26.  
  27.  
  28. def join_force_side(force_book_dict: dict, side: str, user: str):
  29.     if side not in force_book_dict.keys() and not force_user_exists(force_book_dict, user):
  30.         force_book_dict[side] = []
  31.         force_book_dict[side].append(user)
  32.  
  33.     elif not force_user_exists(force_book_dict, user):
  34.         if side not in force_book_dict.keys():
  35.             force_book_dict[side] = []
  36.         force_book_dict[side].append(user)
  37.  
  38.     elif force_user_exists(force_book_dict, user):
  39.         remove_user_from_side(force_book_dict, user)
  40.  
  41.         if side not in force_book_dict.keys():
  42.             force_book_dict[side] = []
  43.  
  44.         force_book_dict[side].append(user)
  45.  
  46.     print(f'{user} joins the {side} side!')
  47.  
  48.  
  49. force_book = {}
  50.  
  51. command = input()
  52. while command != 'Lumpawaroo':
  53.     if '|' in command:
  54.         details = command.split(' | ')
  55.         force_side = details[0]
  56.         force_user = details[1]
  57.  
  58.         initial_join_force_side(force_book, force_side, force_user)
  59.  
  60.     elif '->' in command:
  61.         details = command.split(' -> ')
  62.         force_user = details[0]
  63.         force_side = details[1]
  64.  
  65.         join_force_side(force_book, force_side, force_user)
  66.  
  67.     command = input()
  68.  
  69. for key, value in force_book.items():
  70.     if len(value) > 0:
  71.         print(f'Side: {key}, Members: {len(value)}')
  72.         for el in value:
  73.             print(f"! {el}")
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement