diff --git "a/HW1_\347\216\213\344\272\255\345\240\205.py" "b/HW1_\347\216\213\344\272\255\345\240\205.py" new file mode 100644 index 0000000..af38efc --- /dev/null +++ "b/HW1_\347\216\213\344\272\255\345\240\205.py" @@ -0,0 +1,75 @@ +#開啟、讀取文件檔 +article = open('2022summer-git-python_basics/HW1/HW1.txt','r',encoding='utf-8') +content = article.read() +#print(content) +article.close() + +#計算句子個數 +sentence_list = content.split('.') +del sentence_list[len(sentence_list)-1]#最後一個元素會是'',需刪除 +sentence_num = len(sentence_list) + +#建立句子長度統計器 +def counter(sentence_list, characters = None, trim = False): + a=0 + b=0 + c=0 + d=0 + e=0 + f=0 + num_stc_with_characters = 0 + detector = False + + for stc in sentence_list: + len_stc = len(stc)+1 + + if trim: + # characters = list(characters) + for x in characters: + if x in stc: + stc = stc.replace(x,'') + detector = True + len_stc = len(stc)+1 + if detector: + num_stc_with_characters += 1 + detector = False + + if len_stc <= 40: + a+=1 + elif len_stc <= 50: + b+=1 + elif len_stc <= 60: + c+=1 + elif len_stc <= 70: + d+=1 + elif len_stc <= 80: + e+=1 + else: + f+=1 + + if not trim: + return a, b, c, d, e, f + return a, b, c, d, e, f, num_stc_with_characters + +#計算句子長度 +#修除亂碼前 +a, b, c, d, e, f = counter(sentence_list) +print(a+b+c+d+e+f == sentence_num) + +#修除亂碼後 +characters = '!?+-' +g, h, i, j, k, l, num = counter(sentence_list, characters, trim = True) +print(g+h+i+j+k+l == sentence_num) + + +#將結果輸出成文件檔 +HW1 = open('2022summer-git-python_basics/HW1_王亭堅.txt','w') +HW1.write(f'There are {sentence_num} sentences in HW1.txt. {num} of them include !?+-.\n') +HW1.write('length before trim after trim\n') +HW1.write(f' <=40 {a:11d}{g:11d}\n') +HW1.write(f' <=50 {b:11d}{h:11d}\n') +HW1.write(f' <=60 {c:11d}{i:11d}\n') +HW1.write(f' <=70 {d:11d}{j:11d}\n') +HW1.write(f' <=80 {e:11d}{k:11d}\n') +HW1.write(f' >80 {f:11d}{l:11d}\n') +HW1.close() \ No newline at end of file diff --git "a/HW1_\347\216\213\344\272\255\345\240\205.txt" "b/HW1_\347\216\213\344\272\255\345\240\205.txt" new file mode 100644 index 0000000..92f083f --- /dev/null +++ "b/HW1_\347\216\213\344\272\255\345\240\205.txt" @@ -0,0 +1,8 @@ +There are 539 sentences in HW1.txt. 170 of them include !?+-. +length before trim after trim + <=40 27 37 + <=50 161 196 + <=60 156 138 + <=70 121 115 + <=80 61 50 + >80 13 3 diff --git "a/HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.py" "b/HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.py" new file mode 100644 index 0000000..c2327b8 --- /dev/null +++ "b/HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.py" @@ -0,0 +1,77 @@ +#開啟、讀取文件檔 +article = open('2022summer-git-python_basics/HW1/HW1.txt','r') +content = article.read() +#print(content) +article.close() + +#計算句子個數 +sentence_list = content.split('.') +del sentence_list[len(sentence_list)-1]#最後一個元素會是'',需刪除 +sentence_num = len(sentence_list) + +#建立句子長度統計器 +def counter(sentence_list, characters = None, trim = False): + a=0 + b=0 + c=0 + d=0 + e=0 + f=0 + num_stc_with_characters = 0 + detector = False + + for stc in sentence_list: + + if trim: + for x in characters: + if x in stc: + stc = stc.replace(x,'') + detector = True + if detector: + num_stc_with_characters += 1 + detector = False + + if stc == sentence_list[0]: + len_stc = len(stc)+1#第一句,補上拆分時去掉的句點 + else: + len_stc = len(stc)-1#其他句,補上拆分時去掉的句點,然後消除換行符號'\n'佔據的長度 + + if len_stc <= 40: + a+=1 + elif len_stc <= 50: + b+=1 + elif len_stc <= 60: + c+=1 + elif len_stc <= 70: + d+=1 + elif len_stc <= 80: + e+=1 + else: + f+=1 + + if not trim: + return a, b, c, d, e, f + return a, b, c, d, e, f, num_stc_with_characters + +#計算句子長度 +#修除亂碼前 +a, b, c, d, e, f = counter(sentence_list) +print(a+b+c+d+e+f == sentence_num) + +#修除亂碼後 +characters = '!?+-' +g, h, i, j, k, l, num = counter(sentence_list, characters, trim = True) +print(g+h+i+j+k+l == sentence_num) + + +#將結果輸出成文件檔 +HW1 = open('2022summer-git-python_basics/HW1_王亭堅/HW1_王亭堅.txt','w') +HW1.write(f'There are {sentence_num} sentences in HW1.txt. {num} of them include !?+-.\n') +HW1.write('length before trim after trim\n') +HW1.write(f' <=40 {a:11d}{g:11d}\n') +HW1.write(f' <=50 {b:11d}{h:11d}\n') +HW1.write(f' <=60 {c:11d}{i:11d}\n') +HW1.write(f' <=70 {d:11d}{j:11d}\n') +HW1.write(f' <=80 {e:11d}{k:11d}\n') +HW1.write(f' >80 {f:11d}{l:11d}\n') +HW1.close() \ No newline at end of file diff --git "a/HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.txt" "b/HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.txt" new file mode 100644 index 0000000..b07ddfc --- /dev/null +++ "b/HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.txt" @@ -0,0 +1,8 @@ +There are 539 sentences in HW1.txt. 170 of them include !?+-. +length before trim after trim + <=40 47 61 + <=50 173 194 + <=60 152 142 + <=70 111 108 + <=80 48 33 + >80 8 1 diff --git a/name.txt b/name.txt new file mode 100644 index 0000000..5989bbc --- /dev/null +++ b/name.txt @@ -0,0 +1 @@ +i am Ting-Jian. \ No newline at end of file