From aed9e95cd1b75c80661c20f625a32704226414e9 Mon Sep 17 00:00:00 2001 From: Kenny Date: Tue, 5 Jul 2022 06:53:05 +0000 Subject: [PATCH 1/4] Tell you my name. --- name.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 name.txt 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 From ea174b6d6de3e05b7428cafccdada8c92998b377 Mon Sep 17 00:00:00 2001 From: Kenny Date: Mon, 11 Jul 2022 10:39:13 +0000 Subject: [PATCH 2/4] HW1 --- "HW1_\347\216\213\344\272\255\345\240\205.py" | 75 +++++++++++++++++++ ..._\347\216\213\344\272\255\345\240\205.txt" | 8 ++ 2 files changed, 83 insertions(+) create mode 100644 "HW1_\347\216\213\344\272\255\345\240\205.py" create mode 100644 "HW1_\347\216\213\344\272\255\345\240\205.txt" 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 From 3c7bd696b4aec3f97672879eb88724cc9776e5a7 Mon Sep 17 00:00:00 2001 From: Kenny Date: Mon, 11 Jul 2022 10:49:18 +0000 Subject: [PATCH 3/4] =?UTF-8?q?HW1=5F=E7=8E=8B=E4=BA=AD=E5=A0=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1_\347\216\213\344\272\255\345\240\205.py" | 75 +++++++++++++++++++ ..._\347\216\213\344\272\255\345\240\205.txt" | 8 ++ 2 files changed, 83 insertions(+) create mode 100644 "HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.py" create mode 100644 "HW1_\347\216\213\344\272\255\345\240\205/HW1_\347\216\213\344\272\255\345\240\205.txt" 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..8abcb93 --- /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,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_王亭堅/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..92f083f --- /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 27 37 + <=50 161 196 + <=60 156 138 + <=70 121 115 + <=80 61 50 + >80 13 3 From 8e024e27aad8f963e2af957eb7211d22ec520e2e Mon Sep 17 00:00:00 2001 From: Kenny Date: Mon, 11 Jul 2022 11:16:45 +0000 Subject: [PATCH 4/4] =?UTF-8?q?HW1=5F=E7=8E=8B=E4=BA=AD=E5=A0=85(=E4=BF=AE?= =?UTF-8?q?=E6=94=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HW1_\347\216\213\344\272\255\345\240\205.py" | 14 ++++++++------ .../HW1_\347\216\213\344\272\255\345\240\205.txt" | 12 ++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) 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" index 8abcb93..c2327b8 100644 --- "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" @@ -1,5 +1,5 @@ #開啟、讀取文件檔 -article = open('2022summer-git-python_basics/HW1/HW1.txt','r',encoding='utf-8') +article = open('2022summer-git-python_basics/HW1/HW1.txt','r') content = article.read() #print(content) article.close() @@ -21,19 +21,21 @@ def counter(sentence_list, characters = None, trim = False): 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 + 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: 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" index 92f083f..b07ddfc 100644 --- "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" @@ -1,8 +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 + <=40 47 61 + <=50 173 194 + <=60 152 142 + <=70 111 108 + <=80 48 33 + >80 8 1