設計說明:
請撰寫一程式,依序輸入五個、三個、九個整數,並各自儲存到集合set1、set2、set3中。接著回答:set2是否為set1的子集合(subset)?set3是否為set1的超集合(superset)?
輸入輸出:
輸入說明
依序分別輸入五個、三個、九個整數
輸出說明
顯示回覆:
set2是否為set1的子集合(subset)?
set3是否為set1的超集合(superset)?
set2是否為set1的子集合(subset)?
set3是否為set1的超集合(superset)?
輸入輸出範例
輸入與輸出會交雜如下,輸出的部份以粗體字表示
Input to set1:
3
28
-2
7
39
Input to set2:
2
77
0
Input to set3:
3
28
12
99
39
7
-1
-2
65
set2 is subset of set1: False
set3 is superset of set1: True
3
28
-2
7
39
Input to set2:
2
77
0
Input to set3:
3
28
12
99
39
7
-1
-2
65
set2 is subset of set1: False
set3 is superset of set1: True
Code:
S1,S2,S3=set(), set(), set() print('Input to set1:') for i in range(5): S1.add(eval(input())) print('Input to set2:') for i in range(3): S2.add(eval(input())) print('Input to set3:') for i in range(9): S3.add(eval(input())) print('set2 is subset of set1: ',end="") if S2.issubset(S1)==True: print("True") else: print("False") print('set3 is superset of set1: ',end="") if S3.issuperset(S1)==True: print("True") else: print("False")