TQC+ 程式語言 Python 3 _ 705 子集合與超集合

說明:
請撰寫一程式,依序輸入五個、三個、九個整數,並各自儲存到集合set1、set2、set3中。接著回答: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


程式碼:
set1, set2, set3 = set(), set(), set()

print('Input to set1:')
for i in range(5): set1.add( eval( input() ) )

print('Input to set2:')
for i in range(3): set2.add( eval( input() ) )

print('Input to set3:')
for i in range(9): set3.add( eval( input() ) )

print('set2 is subset of set1:', set2<=set1 )
print('set3 is superset of set1:', set3>=set1 )

沒有留言:

張貼留言