Read two sets of integers A and B and print the results of the set operations.
Input
Two lines, each with 0 to 50 integers separated by spaces: the elements of A, then of B. A number may be repeated; it is still one element. A line may be empty: the empty set.
Output
Five lines:
union: ...
intersection: ...
A minus B: ...
symmetric difference: ...
subsets of A: N
Each set is written as its elements in increasing order, separated by spaces, or {} if it is empty. N is the number of subsets of A.
Examples
Example 1 · Input
1 2 3 4 3 4 5
Expected output
union: 1 2 3 4 5 intersection: 3 4 A minus B: 1 2 symmetric difference: 1 2 5 subsets of A: 16
Example 2 · Input
7 7 7 1 7
Expected output
union: 1 7
intersection: 7
A minus B: {}
symmetric difference: 1
subsets of A: 2
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
set(map(int, line.split())) makes a set, and drops repeats.
Hint 2
| & - ^ are union, intersection, difference and symmetric difference.
Hint 3
Print sorted(...) joined with spaces, or {} for an empty set. A set of n elements has 2**n subsets.
Solution
Show one correct solution (try it yourself first)
A = set(map(int, input().split()))
B = set(map(int, input().split()))
def show(s):
return " ".join(map(str, sorted(s))) if s else "{}"
print("union:", show(A | B))
print("intersection:", show(A & B))
print("A minus B:", show(A - B))
print("symmetric difference:", show(A ^ B))
print("subsets of A:", 2 ** len(A))