# Python TQC 701~710 ## 701 串列數組轉換 ### 說明: 請撰寫一程式,輸入數個整數並儲存至串列中,以輸入-9999為結束點(串列中不包含-9999),再將此串列轉換成數組,最後顯示該數組以及其長度(Length)、最大值(Max)、最小值(Min)。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/5s9zhOx.png) 範例輸出: ![](https://i.imgur.com/1TZHPps.png) ### Code: ```python= LIST = [] num = eval(input()) while num!=-9999: LIST.append(num) num = eval(input()) TUPLE = tuple(LIST) print(TUPLE) print('Lenght:',len(TUPLE)) print('Max:',max(TUPLE)) print('min:',min(TUPLE)) ``` ## 702 數組合併排序 ### 說明: 請撰寫一程式,輸入並建立兩組數組,各以-9999為結束點(數組中不包含-9999)。將此兩數組合併並從小到大排序之,顯示排序前的數組和排序後的串列。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/0mFYTFV.png) 範例輸出: ![](https://i.imgur.com/uNSSMyb.png) ### Code: ```python= tuple1 = () print('Create tuple 1:') num = eval(input()) while num !=-9999: tuple1 +=(num,) num = eval(input()) tuple2 = () print('Create tuple 2:') num = eval(input()) while num !=-9999: tuple2 +=(num,) num = eval(input()) tuple1 += tuple2 print('Combined tuple before sorting:',tuple1) LIST = list(tuple1) LIST.sort() print('Combined list after sorting:',LIST) ``` ## 703 數組條件判斷 ### 說明: 請撰寫一程式,輸入一些字串至數組(至少輸入五個字串),以字串"end"為結束點(數組中不包含字串"end")。接著輸出該數組,再分別顯示該數組的第一個元素到第三個元素和倒數三個元素。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/GE7Np41.png) 範例輸出: ![](https://i.imgur.com/8eKuwvK.png) ### Code: ```python= TUPLE = () string = input() while string != "end": TUPLE +=(string,) string = input() print(TUPLE) print(TUPLE[0:3]) print(TUPLE[-3:]) ``` ## 704 集合條件判斷 ### 說明: 請撰寫一程式,輸入數個整數並儲存至集合,以輸入-9999為結束點(集合中不包含-9999),最後顯示該集合的長度(Length)、最大值(Max)、最小值(Min)、總和(Sum)。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/C6zTtOF.png) 範例輸出: ![](https://i.imgur.com/p4KfknQ.png) ### Code: ```python= set1 = set() num = eval(input()) while num!=-9999: set1.add(num) num = eval(input()) print('Lenght:',len(set1)) print('Max:',max(set1)) print('min:',min(set1)) print('Sum:',sum(set1)) ``` ## 705 子集合與超集合 ### 說明: 請撰寫一程式,依序輸入五個、三個、九個整數,並各自儲存到集合set1、set2、set3中。接著回答:set2是否為set1的子集合(subset)?set3是否為set1的超集合(superset)? ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/mHTYcNo.png) ![](https://i.imgur.com/Gqt4vko.png) 範例輸出: ![](https://i.imgur.com/GuvxmuI.png) ### Code: ```python= set1 = set() set2 = set() set3 = 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 sibset of set1:',set2.issubset(set1)) print('set3 is sibset of set1:',set3.issuperset(set1)) ``` ## 706 全字母句 ### 說明: 全字母句(Pangram)是英文字母表所有的字母都出現至少一次(最好只出現一次)的句子。請撰寫一程式,要求使用者輸入一正整數k(代表有k筆測試資料),每一筆測試資料為一句子,程式判斷該句子是否為Pangram,並印出對應結果True(若是)或False(若不是)。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/xRejUKK.png) 範例輸出: ![](https://i.imgur.com/OEUZtuj.png) ### Code: ```python= count= eval(input()) alpha=26 for i in range(count): sentence = input() set1 = set(sentence.lower()) if ' ' in set1: set1.remove(' ') if len(set1) >= alpha: print('True') else: print('False') ``` ## 707 共同科目 ### 說明: 請撰寫一程式,輸入X組和Y組各自的科目至集合中,以字串"end"作為結束點(集合中不包含字串"end")。請依序分行顯示(1) X組和Y組的所有科目、(2)X組和Y組的共同科目、(3)Y組有但X組沒有的科目,以及(4) X組和Y組彼此沒有的科目(不包含相同科目)。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/NYnb0fr.png) 範例輸出: ![](https://i.imgur.com/MiHXSoM.png) ### Code: ```python= xS = set() yS = set() print('Enter group X subject:') x = input() while x != 'end': xS.add(x) x=input() print('Enter group Y subject:') y = input() while y != 'end': yS.add(y) y=input() print(sorted(xS.union(yS))) print(sorted(yS.intersection(xS))) print(sorted(yS.difference(xS))) print(sorted(yS.symmetric_difference(xS))) ``` ## 708 詞典合併 ### 說明: 請撰寫一程式,自行輸入兩個詞典(以輸入鍵值"end"作為輸入結束點,詞典中將不包含鍵值"end"),將此兩詞典合併,並根據key值字母由小到大排序輸出,如有重複key值,後輸入的key值將覆蓋前一key值。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/330HmUY.png) 範例輸出: ![](https://i.imgur.com/TawdADI.png) ### Code: ```python= xDIC={} yDIC={} print('Create dict1:') while True: key = input('Key:') if key == 'end': break xDIC[key] = input('Value:') print('Create dict2:') while True: key = input('Key:') if key == 'end': break yDIC[key] = input('Value:') xDIC.update(yDIC) for i in sorted(xDIC.keys()): print(i,': ',xDIC[i]) ``` ## 709 詞典排序 ### 說明: 請撰寫一程式,輸入一顏色詞典color_dict(以輸入鍵值"end"作為輸入結束點,詞典中將不包含鍵值"end"),再根據key值的字母由小到大排序並輸出。 ### 輸入/輸出 範例輸入: ![](https://i.imgur.com/MzQePua.png) 範例輸出: ![](https://i.imgur.com/1pdLNTZ.png) ### Code: ```python= color_dict = {} while True: key = input('Key:') if key == 'end': break color_dict[key] = input('Value:') for i in sorted(color_dict.keys()): print(i,': ',color_dict[i]) ``` ## 710 詞典搜尋 ### 說明: 請撰寫一程式,為一詞典輸入資料(以輸入鍵值"end"作為輸入結束點,詞典中將不包含鍵值"end"),再輸入一鍵值並檢視此鍵值是否存在於該詞典中。 ### 輸入/輸出 範例輸入/輸出: ![](https://i.imgur.com/AA3eMht.png) ### Code: ```python= data_dist = {} while True: key = input('Key:') if key == 'end': break data_dist[key] = input('Value:') search = input('Search key:') print(search in data_dist.keys()) ``` --- 相關文章: [Python TQC 701~710](https://hackmd.io/@Neroal/BkHTyP6v8) ###### tags: `python`