CodeSolved

Solved Programming Questions & Exercises

Subscription and community of two sets

Practice Easy 1744/ Download 852 Views

Write a program that receives two sets of entry and shows its subscription and community


Help: The two sets are meant to collect members of both A and B sets in one collection without repeating members.
Help: The purpose of subscribing to the two sets is to collect members of both sets A and B that are in both A and B in a collection.


Example:

Receive Input:

Input1: 1,2,3,4
Input2: 3,4,5,6

Output:

1,2,3,4,5,6
3,4

9 Answers

A = {1,2,3,10,11,12,32,33}
B = {1,2,3,14,10,54,354,121}
x = A.union(B)
y = A.intersection(B)
Z = B.union(A)
S = B.intersection(A)
print(f"upaadat(A)=>{x}")
print(f"eshterak(A)==>{y}")
print(f"update (B)=>{Z}")
print(f"eshterak(B)=>{S}")

Hi the answer

Sumy.amiri Download Python
def set_operations(set1, set2):
    A = set(map(int, set1.split(',')))
    B = set(map(int, set2.split(',')))

    union = A | B
    intersection = A & B

    return ','.join(map(str, sorted(union))), ','.join(map(str, sorted(intersection)))

input1 = "1,2,3,4"
input2 = "3,4,5,6"
union_result, intersection_result = set_operations(input1, input2)

print(union_result)
print(intersection_result)
Ai Download Python
a = {1,2,3,4}
b = {3,4,5,6}
print('community : ',*a.union(b))
print('Subscription :',*a.intersection(b))

set1 = set(map(int, input("input1: ").split(",")))
set2 = set(map(int, input("input2: ").split(",")))
print(",".join(map(str, sorted(set1 | set2))))  # Society
print(",".join(map(str, sorted(set1 & set2))))  # Subscribe

a=set(map(int,input("set1 ").replace(","," ").split()))
b=set(map(int,input("set2 ").replace(","," ").split()))
print(f"union:\n{a|b}")
print(f"unity:\n{a&b}")
try :
    A = []
    B = []
    intersection = []
    share = []

    print('majmoe A : ')
    while True :
        a = input('(-1 for Stop) = ')
        if a == '-1' :
            break
        else :
            A.append(int(a))

    print('=======')

    print('majmoe B : ')
    while True :
        b = input('(-1 for Stop) = ')
        if b == '-1' :
            break
        else :
            B.append(int(b))

    print('----')

    print(f'A : {A}')
    print(f'B : {B}')

    for b in B :
        intersection.append(b)
    for a in A :
        intersection.append(a)
        if a in B :
            share.append(a)

    intersection = list(set(intersection))

    print(f'Intersection : {intersection}')
    print(f'Share : {share}')

except ValueError :
    print('Error : Invalid input .')
Sedvaghefi Download Python
set1 = set(input("Enter Number1 : "))
set2 = set(input("Enter Number2 : "))
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)

print(f"union : {union_set}")
print(f"intersection : {intersection_set}")
User 4207 Download Python
M1 = {3, 4, 5, 6, 8, 9, 11} 
M2 = {5, 22, 31, 62, 93}

def egtema(x, y):
    return x | y

def eshterak(x , y):
    return x & y

print(len(egtema(M1, M2)))
print(len(eshterak(M1, M2)))
def input_set():
    # Receive a collection from the user
    elements = input("عناصر مجموعه را با فاصله از هم وارد کنید: ").split()
    return set(elements)

def main():
    print("مجموعه اول:")
    set_a = input_set()

    print("\nمجموعه دوم:")
    set_b = input_set()

    # Subscription calculation
    intersection = set_a & set_b
    # Community calculation
    union = set_a | set_b

    print("\nنتایج:")
    print(f"اشتراک دو مجموعه: {intersection}")
    print(f"اجتماع دو مجموعه: {union}")

if __name__ == "__main__":
    main()

Submit answer

Submitting answers is currently unavailable.

×
×
Close