# Python題目反思 / 建國中學_邱浚祐 ### Ch4 串列:f071: 2. 刮刮樂 (Lottery)<br> 題目整理:<br> 1. input 3 lucky numbers(integer)<br> 2. input 5 numbers(integer)<br> 3. input 5 amount of money(integer)<br> 4. each number in (2.) is linked to the corresponding money amount in (3.)<br> 5. if previous two lucky number is equal to any number in (2.), we get the corresponding amount of money in (3.)<br> 6. however, if the third lucky number is equal to any number in (2.), we lose the corresponding amount of money in (3.), whereas we won't have negative number in total<br> 7. if the third lucky number is NOT equal to any number in (2.), we can get double amount of money in total<br> 8. output the money that we can get at the end<br> ```python lucky=list(map(int, input().split())) #get lucky num num=list(map(int, input().split())) #get num related to money money=list(map(int, input().split())) #get lottery money luck=True #bool for whether is able to be double up sum=0 #total amount of monry we'll get for i in range(2): for k in range(5): if lucky[i]==num[k]: #whether lucky num is in num zone sum+=money[k] #if True, get corresponding money for k in range(5): if lucky[2]==num[k]: #whether THIRD luck num is in num zone sum-=money[k] #if True, lose corresponding money luck=False # , and will NOT be able to double up if sum<0: print("0") #NO NEGATIVE outputs elif luck==True: print(sum*2) #THIRD lucky num is NOT in num zone, double up else: print(sum) #THIRD lucky num is in num zone ``` 6 25 39 34 6 17 25 29 500 400 300 600 900 2000 ![](https://i.imgur.com/ZMifFBD.png) Further improvement:<br> &ensp;&ensp;use "in" and "index" to solve the problem<br> &ensp;&ensp;c.f. https://stackoverflow.com/questions/9542738/python-find-in-list *** ### Ch5. while迴圈:a149: 乘乘樂<br> 題目整理:<br> 1. get a number which tells how many times of input<br> 2. get an input number<br> 3. print the result that each digit in the number(2.) times each other<br> ```python try: while True: T=int(input()) #get input times as T for i in range(T): #repeat T times n=input() #input num sum=int(n[0]) #sum default as the first digit for i in range(1,len(n)): #for start at second, then to the last digit sum*=int(n[i]) #make sum times number i digit print(sum) #print sum out except EOFError: #EOF(not working in jupyter here) pass ``` 3 356 90 123 6 9999 6561 ![](https://i.imgur.com/ROPv4u3.png) Further improvement:<br> &ensp;&ensp;use list(str) to solve the problem faster<br> &ensp;&ensp;c.f. https://stackoverflow.com/questions/4978787/how-to-split-a-string-into-a-list-of-characters *** ### 04/25 情書解密<br> 題目整理:<br> 1. reverse the fisrt half string and the second half 2. reverse the whole article 3. delete capitalized letters 4. turn "*" to "i", "2" to "to" 5. capitalized letter "I" if it represents first person subject, and turn "_" to "&ensp;"(space) 6. capitalized the first letter that starts a sentence ```python import re s=input() #get the encrypted words n=len(s)//2 #n=length of a half of article chunks = [s[i:i+n] for i in range(0, len(s), n)] #set chunk as a list for two half, ref[1] ans1=chunks[1]+chunks[0] #ans1=reverse the half split ans2=''.join(reversed(ans1)) #reverse the whole article, ref[2] ans3=re.sub(r'[A-Z]', '', ans2) #delete capitalized letters, ref[3] ans4=ans3.replace("*", "i") #replace "*" with "i" ans4=ans4.replace("2", "to") #replace "2" with "to" ans5=ans4.replace(" i ", " I ") #replace "i" with "I" if a space is before and after the letter "i" ans5=ans5.replace("_", " ") #replace "_" with " " ans6=( '. '.join(map(lambda s: s.strip().capitalize(), ans5.split('.')))) #let first letter that starts a sentence be capitalized, ref[4] ans6=ans6.replace(" i ", " I ") #the former line will let the "i" transfer not working so redo the "i" to "I" print(ans6) #print the result ``` WnHCaKeUDmOWT_wYeQn_elohw_a_seTv*g_dnaIKGJ_em_Mserussaer_ed*s_ym_yb_gUn*yatMs_TuoyL_fLo_thgUuHoht_ehtYV.ed*s_ym_yb_uoy_hFt*w_ma_*Y_VetelOpmoc_dnVa_ypSKpah_wIoh_dZeRQz*laUer_QreNvePVKNn_*_.ef*l_PyFm_2J_OgKn* The thought of you staying by my side reassures me and gives a whole new meaning to my life. I never realized how happy and complete I am with you by my side. ref[1]: https://stackoverflow.com/questions/312443/how-do-i-split-a-list-into-equally-sized-chunks, <br>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;https://medium.com/quick-code/python-one-liner-list-comprehension-24e35a20bd1d<br> ref[2]: https://stackoverflow.com/questions/931092/reverse-a-string-in-python<br> ref[3]: https://www.geeksforgeeks.org/python-eliminate-capital-letter-starting-words-from-string<br> ref[4]: https://stackoverflow.com/questions/22800401/how-to-capitalize-the-first-letter-of-every-sentence ![](https://i.imgur.com/YuyFod2.png) Sample Input #1:<br> WnHCaKeUDmOWT_wYeQn_elohw_a_seTv\*g_dnaIKGJ_em_Mserussaer_ed\*s_ym_yb_gUn\*yatMs_TuoyL_fLo_thgUuHoht_ehtYV.ed\*s_ym_yb_uoy_hFt\*w_ma_\*Y_VetelOpmoc_dnVa_ypSKpah_wIoh_dZeRQz\*laUer_QreNvePVKNn_\*_.ef\*l_PyFm_2J_OgKn\* Further improvement:<br> &ensp;&ensp;think a easier way to capitalize the letter that starts a sentence<br> *** ### 考前複習:i096<br> 題目整理:<br> 1. get the number of the stubent(while I wouldn't need this variable) 2. get the amount of lunch budget for each student 3. output how many students are more or equal to the average of budgets ```python try: while True: n=int(input()) total=0 budget=list(map(int, input().split())) for i in range(len(budget)): if budget[i] >= (sum(budget)/len(budget)): total+=1 print(total) except EOFError: pass ``` 5 60 72 35 88 51 2 ![](https://i.imgur.com/nBkIe2q.png) Further improvement:<br> &ensp;&ensp;use sum with for inside print to make code shorter, shown below:<br> &ensp;&ensp;c.f. https://stackoverflow.com/questions/10543303/number-of-values-in-a-list-greater-than-a-certain-number ```python try: while True: n=int(input()) budget=list(map(int, input().split())) print(sum(budget[i] >= (sum(budget)/len(budget)) for i in range(len(budget)))) except EOFError: pass ``` 5 60 72 35 88 51 2 *** 2022.6.23