TQC+ 程式語言 Python 3 _ 707 共同科目

說明:
請撰寫一程式,輸入X組和Y組各自的科目至集合中,以字串"end"作為結束點(集合中不包含字串"end")。請依序分行顯示(1) X組和Y組的所有科目、(2)X組和Y組的共同科目、(3)Y組有但X組沒有的科目,以及(4) X組和Y組彼此沒有的科目(不包含相同科目)。
提示:科目須參考範例輸出樣本,依字母由小至大進行排序。


輸入與輸出會交雜如下,輸出的部份以粗體字表示
Enter group X's subjects:
Math
Literature
English
History
Geography
end
Enter group Y's subjects:
Math
Literature
Chinese
Physical
Chemistry
end
['Chemistry', 'Chinese', 'English', 'Geography', 'History', 'Literature', 'Math', 'Physical']
['Literature', 'Math']
['Chemistry', 'Chinese', 'Physical']
['Chemistry', 'Chinese', 'English', 'Geography', 'History', 'Physical']


程式碼:
x, y = set(), set()

print( "Enter group X's subjects:" )
sub1 = input()
while sub1 != "end":
    x.add( sub1 )
    sub1 = input()

print( "Enter group Y's subjects:" )
sub2 = input()
while sub2 != "end":
    y.add( sub2 )
    sub2 = input()

print ( sorted( x|y ) )
print ( sorted( x&y ) )
print ( sorted( y-x ) )
print ( sorted( x^y ) )

沒有留言:

張貼留言