# Python Ch4-1 ###### tags: `python2021` * TA會將解答公布在題目下方,請同學務必要自己練習 * 要求 (request): * 格式請依照標準 (檔名:hw3-1.py, hw3-2.py...,最後將所有檔案壓縮至學號.zip or 學號.7z上傳e3) * 輸出檔案內容以TA截圖為標準 --- # Question 1 The function of the unfinished program ***Finding_Genius.py*** is to find out among Tom, Alex and several other students, who performed best in an exam. In this program, there are two strings storing student names and scores, which are matched one by one. For instance, the scores of Tom and Alex were 83 and 86, respectively. Finish this program and save it as your own hw3-1.py. ```python= name_string = "Tom,Alex,Jack,Jobs,Alien,Sam" score_string = "83,86,80,95,100,85" list_name = ??? list_score = ??? for i in range(???): ??? max_score = max(list_score) #hint: max() is to find the maximum value in a list max_score_index = list_score.index(???) print(list_name[max_score_index]) ``` ```python= name_string = "Tom,Alex,Jack,Jobs,Alien,Sam" score_string = "83,86,80,95,100,85" list_name = list(name_string.split(",")) list_score = list(score_string.split(",")) for i in range(len(list_score)): list_score[i] = int(list_score[i]) max_score = max(list_score) #hint: max() is to find the maximum value in a list max_score_index = list_score.index(max_score) print(list_name[max_score_index]) ``` :::info Hint: step 1: store the data provided by the strings into lists step 2: for each element in one of the two lists, change the type of variable step 3: find the highest score in the list by **max()** function step 4: find the index of highest score in the list step 5: print out the matched name ::: --- # Question 2 There are several protein sequence fragment files in FASTA format saved in the folder ***protein_fragments***. Write a program to obtain the sequence fragments and concatenate (merge) them in the order of filenames. For instance, the fragment from fragment_1.fasta should be concatenated in front of that from fragment_2.fasta. Save the concatenated sequence into a file named **“protein_x.txt”**. :::warning Note: Your program and the fasta file are **not** in the same folder, so you need to add something to the path if you want to use open(). ::: ```python= f1 = open('.\\protein_fragments\\fragment_1.fasta','r') f2 = open('.\\protein_fragments\\fragment_2.fasta','r') f3 = open('.\\protein_fragments\\fragment_3.fasta','r') f4 = open('.\\protein_fragments\\fragment_4.fasta','r') f5 = open('.\\protein_fragments\\fragment_5.fasta','r') f6 = open('.\\protein_fragments\\protein_x.txt','w') s1 = f1.read() s2 = f2.read() s3 = f3.read() s4 = f4.read() s5 = f5.read() p1 = s1.find("\n") p2 = s2.find("\n") p3 = s3.find("\n") p4 = s4.find("\n") p5 = s5.find("\n") w = s1[p1+1:].replace("\n","")+s2[p2+1:].replace("\n","")+s3[p3+1:].replace("\n","")+s4[p4+1:].replace("\n","")+s5[p5+1:].replace("\n","") f6.write(w) f1.close() f2.close() f3.close() f4.close() f5.close() f6.close() ``` --- # Question 3 The total area of forest on island X is 36000 km^2^ now. Each year in the spring, people living on that island cut 720 km^2^ of the forest to sell the wood and build concrete buildings. The growth rate (or recovery rate) of the forest is known to be 1 percent per year. How large an area of forest will island X still possess 60 years later? ```python= x = 36000 for i in range(60): x = (x - 720)*1.01 print(x) ``` --- # Question 4 Suppose in our lab we have a secret intersexual mouse species with 6 colored strains. They are named Brown, Yellow, Pink, Orange, Red, and Black according to the hair color. Now we are going to mate these strains in all combinations. Please first sort the mouse strains in reverse order of their names and then list all possible mated pairs following the format exemplified below: :::info Hint: An “intersexual” individual is both female and male, meaning that it has female and male reproductive organs at the same time. ::: ![](https://i.imgur.com/eROhc1R.jpg) ```python= name = ['Brown', 'Yellow', 'Pink', 'Orange', 'Red', 'Black'] name.sort(reverse=True) for a in name: for b in name: print(a, 'x' ,b) ```