Advertisement
mbratanov

Untitled

Sep 16th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. from typing import Callable, Dict, Set, List
  2.  
  3.  
  4. def create_set(seq_str: str) -> Set[int]:
  5.     """
  6.    Takes a string of numbers separated by a single space and returns a set of integers.
  7.    :param seq_str: A string containing a sequence of numbers separated by a single space.
  8.    :return: A set of integer numbers
  9.    """
  10.     return {int(x) for x in seq_str.split()}
  11.  
  12.  
  13. def main():
  14.     # Read input.
  15.     set_1: set[int] = create_set(input())
  16.     set_2: set[int] = create_set(input())
  17.  
  18.     n: int = int(input())
  19.  
  20.     # Define functions dictionary with appropriate type hints
  21.     functions: Dict[str, Callable[[List[int]], None]] = {
  22.         "Add First": lambda nums: set_1.update(nums),
  23.         "Add Second": lambda nums: set_2.update(nums),
  24.         "Remove First": lambda nums: set_1.difference_update(nums),
  25.         "Remove Second": lambda nums: set_2.difference_update(nums),
  26.         "Check Subset": lambda _: print(set_1.issubset(set_2) or set_2.issubset(set_1))
  27.     }
  28.  
  29.     # Execute commands
  30.     for _ in range(n):
  31.         user_input: List[str] = input().split()
  32.         current_command: str = f"{user_input[0]} {user_input[1]}"
  33.         numbers: List[int] = [int(num) for num in user_input[2:]]
  34.         functions[current_command](numbers)
  35.  
  36.     # Print output
  37.     print(*sorted(set_1), sep=", ")
  38.     print(*sorted(set_2), sep=", ")
  39.  
  40.  
  41. if __name__ == "__main__":
  42.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement