Advertisement
xi_arma

SUPER FINAL SUMMER GROUP

Aug 13th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. def check_sets(set_A, set_B):
  2.    
  3.     # If the 2 sets are equal
  4.     if set_A == set_B:
  5.         Q1 = "Yes"
  6.     else:
  7.         Q1 = "No"
  8.  
  9.     # If the 2 sets are equivalent
  10.     if len(set_A) == len(set_B):
  11.         Q2 = "Yes"
  12.     else:
  13.         Q2 = "No"
  14.  
  15.     # If the 2 sets are both equal and equivalent
  16.     if set_A == set_B and len(set_A) == len(set_B):
  17.         Q3 = "Yes"
  18.     else:
  19.         Q3 = "No"
  20.  
  21.     return Q1, Q2, Q3
  22.  
  23. def input_value_set(prompt):
  24.     while True:
  25.         user_input = input(prompt)
  26.         values = user_input.split()
  27.        
  28.         # Check duplicate elements
  29.         if len(values) != len(set(values)): # python removes duplicate elements. therefore, this function compares the length of what the user inputted and the set where python deletes the duplicate
  30.             print("You have entered duplicate elements. Please try again.\n")
  31.             continue
  32.          
  33.         if len(values) > 5:
  34.             print("Maximum is 5 elements! Please try again.\n")
  35.            
  36.         else:
  37.             try:
  38.                 return set(map(int, values))
  39.             except ValueError:
  40.                 print("Invalid input. Please enter only integers separated by spaces.\n")
  41.  
  42. # Get input from the user and create sets
  43. set_A = input_value_set("Enter integer values for SET A (separated by spaces): ")
  44. set_B = input_value_set("Enter integer values for SET B (separated by spaces): ")
  45.  
  46. Q1, Q2, Q3 = check_sets(set_A, set_B)
  47.  
  48. print(f"""
  49. A = {set_A}
  50. B = {set_B}
  51.  
  52. Q1. Are the 2 sets equal? {Q1}
  53. Q2. Are the 2 sets equivalent? {Q2}
  54. Q3. Are the 2 sets both equal and equivalent? {Q3}
  55. """)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement