From e044ba51017d4c37e6cef0d52174a47c1f7c995a Mon Sep 17 00:00:00 2001 From: mTattoo Date: Wed, 3 Jul 2019 00:04:15 +0800 Subject: [PATCH 1/8] Notice First of the first : Keep going! --- exercises/1901100039/1001S02E01_helloworld.txt | 2 ++ exercises/1901100039/README.md | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 exercises/1901100039/1001S02E01_helloworld.txt create mode 100644 exercises/1901100039/README.md diff --git a/exercises/1901100039/1001S02E01_helloworld.txt b/exercises/1901100039/1001S02E01_helloworld.txt new file mode 100644 index 000000000..c5dd2d3d0 --- /dev/null +++ b/exercises/1901100039/1001S02E01_helloworld.txt @@ -0,0 +1,2 @@ +Hello World +~。~ \ No newline at end of file diff --git a/exercises/1901100039/README.md b/exercises/1901100039/README.md new file mode 100644 index 000000000..291fb0823 --- /dev/null +++ b/exercises/1901100039/README.md @@ -0,0 +1,2 @@ +# Day 1 +感觉一切有点快又有点晕,跟着视频操练了一遍,学习材料也只是浏览了下,还没有进入状态。加油,少年! \ No newline at end of file From dfc7504a734318876d107024abdd921db92a1759 Mon Sep 17 00:00:00 2001 From: mTattoo Date: Wed, 3 Jul 2019 22:12:23 +0800 Subject: [PATCH 2/8] Create 1001S02E02_hello_python.py First Look --- exercises/1901100039/1001S02E02_hello_python.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100039/1001S02E02_hello_python.py diff --git a/exercises/1901100039/1001S02E02_hello_python.py b/exercises/1901100039/1001S02E02_hello_python.py new file mode 100644 index 000000000..6e441f7ec --- /dev/null +++ b/exercises/1901100039/1001S02E02_hello_python.py @@ -0,0 +1 @@ +print ("Hello World!") \ No newline at end of file From 38b2bfe8f8a855a17aad2555fd5d031dec6cd889 Mon Sep 17 00:00:00 2001 From: mTattoo Date: Thu, 4 Jul 2019 17:29:52 +0800 Subject: [PATCH 3/8] Create 1001S02E03_calculator.py --- exercises/1901100039/1001S02E03_calculator.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 exercises/1901100039/1001S02E03_calculator.py diff --git a/exercises/1901100039/1001S02E03_calculator.py b/exercises/1901100039/1001S02E03_calculator.py new file mode 100644 index 000000000..1e33d8829 --- /dev/null +++ b/exercises/1901100039/1001S02E03_calculator.py @@ -0,0 +1,37 @@ +# 杩欐槸涓涓彧鑳借繘琛屽姞鍑忎箻闄よ繍绠楃殑鍒濈骇璁$畻鍣ㄧ▼搴 +# 鎻愮ず鐢ㄦ埛杈撳叆杩愮畻绗︼紝鍒ゆ柇鏄惁姝g‘杈撳叆锛屽苟灏嗘暟鍊煎瓨鍏ュ搴斿彉閲 +while True: + operator = input('璇疯緭鍏ヨ繍绠楃锛+銆-銆*銆/锛: ') + if operator == '+' or operator == '-' or operator == '*' or operator == '/': + break + else: + print('璇疯緭鍏ユ纭殑杩愮畻绗') + continue +# 鎻愮ず鐢ㄦ埛杈撳叆鏁板瓧锛屽垽鏂槸鍚︽纭緭鍏ワ紝骞跺皢鏁板煎瓨鍏ュ搴斿彉閲 +while True: + Number_One = input('璇疯緭鍏ョ涓涓暟瀛楋細') + if Number_One.isdigit(): + break + else: + print('璇疯緭鍏ユ纭殑鏁板瓧') + continue +while True: + Number_Two = input('璇疯緭鍏ョ浜屼釜鏁板瓧锛') + if Number_Two.isdigit(): + break + else: + print('璇疯緭鍏ユ纭殑鏁板瓧') + continue +# 灏嗘暟瀛楀彉閲忕殑瀛楃绫诲瀷鐢'Str'杞崲涓'Int' +a = int(Number_One) +b = int(Number_Two) + +# 鎸夎繍绠楄鍒欒繘琛岃繍绠楀苟杈撳嚭缁撴灉 +if operator == '+': + print(a,'+',b,'=',a + b) +elif operator == '-': + print(a,'-',b,'=',a - b) +elif operator == '*': + print(a,'*',b,'=',a * b) +elif operator == '/': + print(a,'梅',b,'=',a / b) \ No newline at end of file From ab5618d737b64afca15f83bcd0f612ff31da3ed6 Mon Sep 17 00:00:00 2001 From: mTattoo Date: Fri, 5 Jul 2019 18:29:12 +0800 Subject: [PATCH 4/8] Create 1001S02E04_control_flow.py --- exercises/1901100039/1001S02E04_control_flow.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 exercises/1901100039/1001S02E04_control_flow.py diff --git a/exercises/1901100039/1001S02E04_control_flow.py b/exercises/1901100039/1001S02E04_control_flow.py new file mode 100644 index 000000000..f338b4782 --- /dev/null +++ b/exercises/1901100039/1001S02E04_control_flow.py @@ -0,0 +1,14 @@ +# 值浣跨敤for...in 寰幆鎵撳嵃涔濅節涔樻硶琛紝杈撳嚭锛 +for i in range(1,10): + for j in range(1, i + 1): + print( i,'*',j ,'=', i * j, end='\t') + print() + +# 浣跨敤while寰幆鎵撳嵃涔濅節涔樻硶琛ㄥ苟鐢ㄦ潯浠跺垽鏂妸鍋舵暟琛屽幓鎺 +for i in range(1,10): + if (i % 2) == 0: + continue + for j in range(1, i + 1): + print ( i,'*',j ,'=', i * j, end='\t') + print() + \ No newline at end of file From 69110a550d92f822a44d0110de010d186d2afb16 Mon Sep 17 00:00:00 2001 From: mTattoo Date: Fri, 12 Jul 2019 16:53:08 +0800 Subject: [PATCH 5/8] Day 05 --- exercises/1901100039/1001S02E05_array.py | 37 +++++++++++++ exercises/1901100039/1001S02E05_stats_text.py | 52 +++++++++++++++++++ exercises/1901100039/1001S02E05_string.py | 47 +++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 exercises/1901100039/1001S02E05_array.py create mode 100644 exercises/1901100039/1001S02E05_stats_text.py create mode 100644 exercises/1901100039/1001S02E05_string.py diff --git a/exercises/1901100039/1001S02E05_array.py b/exercises/1901100039/1001S02E05_array.py new file mode 100644 index 000000000..f0746b01e --- /dev/null +++ b/exercises/1901100039/1001S02E05_array.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Fri Jul 12 16:21:21 2019 + +@author: mtattoo +""" + +# 瀹氫箟鏁扮粍 +list_0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +list_1 = list_0[::-1] # 閫嗚浆鏁扮粍 +print(list_1) + +# 鎷兼帴瀛楃涓 +list_2 = [str(i) for i in list_1]# 浣跨敤鍒楄〃鎺ㄥ寮忔妸鍒楄〃涓殑鍗曚釜鍏冪礌鍏ㄩ儴杞寲涓簊tr绫诲瀷 +list_3 = ''.join(list_2) +print (list_3) + +# 鍙栧嚭绗笁鍒扮鍏釜瀛楃 +list_4 = list_3[2:8] # 瀛楃涓蹭互0寮濮嬶紝鍙傛暟鏈鍚庝竴浣嶆暟鎹笉璁″叆鍒楄〃锛屾晠涓嶆槸2:7 +print (list_4) + +# 瀵瑰瓧绗︿覆杩涜缈昏浆 +list_r = list_4[::-1] +print (list_r) + +# 灏嗙粨鏋滆浆鍖栦负int绫诲瀷 + +int_value = int(list_r) + +print(int_value) + +# 灏嗙粨鏋滆浆鍖栦负浜岃繘鍒讹紝鍏繘鍒跺拰鍗佸叚杩涘埗锛屽苟杈撳嚭 + +print("杞崲涓轰簩杩涘埗鐨勫硷細", bin(int_value)) +print("杞崲涓哄叓杩涘埗鐨勫硷細", oct(int_value)) +print("杞崲涓哄崄鍏繘鍒剁殑鍊硷細", hex(int_value)) \ No newline at end of file diff --git a/exercises/1901100039/1001S02E05_stats_text.py b/exercises/1901100039/1001S02E05_stats_text.py new file mode 100644 index 000000000..28c4a7331 --- /dev/null +++ b/exercises/1901100039/1001S02E05_stats_text.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Mon Jul 8 18:10:33 2019 + +@author: mtattoo +""" + +# 瀛楃涓茬殑缁熻 + +text =''' +The Zen of Python, by Tim Peters + + +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambxiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + +# 缁熻鏂囨湰涓崟璇嶅嚭鐜扮殑娆℃暟 + +import re #寮曞叆搴 +text_pure = re.sub(r'[^\w\s]','',text) #姝e垯琛ㄨ揪寮忚繘琛屽墧闄 +words = text_pure.split() # 鎷嗗垎鎵鏈夊崟璇 + +frequency= {} # 瀹氫箟棰戠巼瀛楀吀 +for word in words: + if word not in frequency: + frequency [word] = 1 + else: + frequency [word] += 1 +print (frequency) + +# 鎸夌粺璁℃鏁拌緭鍑 +frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) +print(frequency_order) diff --git a/exercises/1901100039/1001S02E05_string.py b/exercises/1901100039/1001S02E05_string.py new file mode 100644 index 000000000..e401892ca --- /dev/null +++ b/exercises/1901100039/1001S02E05_string.py @@ -0,0 +1,47 @@ +# 瀛楃涓茬殑澶勭悊缁冧範 + +text =''' +The Zen of Python, by Tim Peters + + +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambxiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +''' + +print (text) + +# 灏嗕笂鏂囦腑鐨勨渂etter鈥濇浛鎹负鈥渨orse鈥 +text_worse = text.replace("better","worse") +print (text_worse) + +# 灏嗕笂鏂囦腑鐨勨渆a鈥濆墧闄 +text_noea = text_worse.replace("ea","") +print (text_noea) + + +# 灏嗕笂鏂囦腑鐨勫瓧姣嶈繘琛屽ぇ灏忓啓缈昏浆 +text_swap = text_noea.swapcase() +print (text_swap) + +# 瀵逛笂鏂囦腑鐨勫崟璇嶈繘琛屾帓搴忓苟杈撳嚭 +import re #寮曞叆搴 +s = re.sub(r'[^\w\s]','',text_noea) #姝e垯琛ㄨ揪寮忚繘琛屽墧闄 +text_sort = sorted(s.split()) +print (text_sort) \ No newline at end of file From 29a8fc85f8262ef72019323fbe8f9b863a55cafe Mon Sep 17 00:00:00 2001 From: mTattoo Date: Mon, 15 Jul 2019 17:27:40 +0800 Subject: [PATCH 6/8] Create 1001S02E06_stats_word.py --- exercises/1901100039/1001S02E06_stats_word.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 exercises/1901100039/1001S02E06_stats_word.py diff --git a/exercises/1901100039/1001S02E06_stats_word.py b/exercises/1901100039/1001S02E06_stats_word.py new file mode 100644 index 000000000..55006a1b9 --- /dev/null +++ b/exercises/1901100039/1001S02E06_stats_word.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Mon Jul 15 15:31:31 2019 + +@author: mtattoo +""" +text = input('璇疯緭鍏ヨ嫳鏂囨枃鏈:') + +# 灏佽缁熻鑻辨枃鍗曡瘝璇嶉鐨勫嚱鏁 +def stats_text_en( text ): + # 缁熻杈撳叆鑻扁絺鍗曡瘝鍑虹幇鐨勬鏁帮紝鏈鍚庤繑鍥炩紑涓寜璇嶉闄嶅簭鎺掑垪铴滅殑鏁扮粍 + import re #寮曞叆搴 + text_pure = re.sub(r'[^\w\s]','',text) #姝e垯琛ㄨ揪寮忚繘琛屽墧闄 + words = text_pure.split() # 鎷嗗垎鎵鏈夊崟璇 + + frequency= {} # 瀹氫箟棰戠巼瀛楀吀 + for word in words: + if word not in frequency: + frequency [word] = 1 + else: + frequency [word] += 1 + frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) + print('鎮ㄦ娆¤緭鍏ョ殑鏂囨湰璇嶉闄嶅簭鎺掑垪濡備笅:', frequency_order) +stats_text_en( text ) # 鎵ц灏佽鍑芥暟 + + +text = input('璇疯緭鍏ヤ腑鏂囨枃鏈:') +# 灏佽缁熻涓枃姹夊瓧娆℃暟鐨勫嚱鏁 +def stats_text_cn( text ): + cn_characters = [] + for character in text: + # 浠nicode涓腑鏂囧瓧绗﹀瓨鍌ㄧ殑鑼冨洿 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + frequency= {} # 瀹氫箟棰戠巼瀛楀吀 + for cn_characters in text: + if cn_characters not in frequency: + frequency [cn_characters] = 1 + else: + frequency [cn_characters] += 1 + frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True) + print('鎮ㄦ娆¤緭鍏ョ殑鏂囨湰璇嶉闄嶅簭鎺掑垪濡備笅:', frequency_order) + +stats_text_cn( text ) # 鎵ц灏佽鍑芥暟 \ No newline at end of file From 6ae51267ec69f9046d15a59b09fa05d813e80e5c Mon Sep 17 00:00:00 2001 From: mTattoo Date: Wed, 17 Jul 2019 17:08:02 +0800 Subject: [PATCH 7/8] Day 07 Good job --- exercises/1901100039/d07/main.py | 46 +++++++++++++++++++ .../1901100039/d07/mymodule/stats_word.py | 46 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 exercises/1901100039/d07/main.py create mode 100644 exercises/1901100039/d07/mymodule/stats_word.py diff --git a/exercises/1901100039/d07/main.py b/exercises/1901100039/d07/main.py new file mode 100644 index 000000000..e148e2f00 --- /dev/null +++ b/exercises/1901100039/d07/main.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Wed Jul 17 15:06:36 2019 + +@author: mtattoo +""" + +from mymodule import stats_word +dir() + +text = ''' +鎰氬叕绉诲北 + +澶▓锛岀帇灞嬧紗灞辩殑鍖楅潰锛屼綇浜嗭涓鍊嬩節饧楁鐨勨郊缈侊紝鍚嶅彨鎰氬叕銆傗紗灞变綌鍦板唬闂婏紝鎿嬩綇鍘伙し锛屼娇浠栧拰瀹朵汉寰铯サ鐐轰笉铳ャ +涓澶╋紝鎰氬叕鍙泦瀹朵汉瑾:銆岃畵鎴戝戝悇鐩″叾铴婏紝鍓峰钩饧嗗北锛岄枊姊濋亾铯凤紝鐩撮氳鲍宸烇紝浣犲戣獚鐐烘庢ǎ?銆 +澶у閮界暟鍙e悓鑱茶磰鎴愶紝鍙湁浠栫殑濡诲瓙琛ㄧず鎳风枒锛屼甫瑾:銆屼綘閫i枊閼夸竴鍊嬧缉涓樼殑饧掞ゾ閮芥矑鏈夛紝鎬庡彲鑳藉壏骞冲お铷堛佺帇灞嬩簩灞卞憿? 娉佷笖锛岄懣鍑虹殑饧熺煶鍙堜笩鍒板摢瑁忓幓鍛?銆 +饧ゅ閮界啽铴熷湴瑾:銆屾妸饧熺煶涓熼叉袱铹呰銆傘 +鏂兼槸鎰氬叕灏卞拰鍏掑锛屸紑璧烽枊鎸栧湡锛屾妸饧熺煶鎼亱鍒版袱铹呭幓銆 +鎰氬叕鐨勯劙灞呮槸鍊嬪濠︼紝鏈夊嬪厭饧﹀叓姝蹭篃鑸堣嚧鍕冨媰鍦扳緵铯公蹇欍 +瀵掍締鏆戝線锛屼粬鍊戣饧铴庢墠鑳藉線杩旀袱铹呬竴娆° +浣忓湪饪堟渤娌崇晹鐨勬櫤鍙燂紝鐪嬧緬浠栧戦欐ǎ杈涜嫤锛屽彇绗戞剼鍏:銆屼綘涓嶆槸寰堟剼锠㈠棊?浣犲凡饧鎶婂勾绱铴猴紝灏辨槸饨ょ洝浣犵殑姘b紥锛屼篃涓嶈兘鎸栧幓灞辩殑饧瑙掑憿?銆 +鎰氬叕姝庢伅閬:銆屼綘鏈夐欐ǎ鐨勬垚饩掞紝鏄ェ鏈冩槑鐧界殑銆備綘饨愰偅瀵″│鐨勫皬鍏掆鸡閭勶ェ濡傚憿!灏辩畻鎴戞铴猴紝閭勬湁鎴戠殑鍏掑瓙锛屾垜鐨勫瀛愶紝鎴戠殑鏇惧瀛愶紝浠栧戔紑鐩村偝涓嬪幓銆傗浇閫欌紗灞辨槸铳ф渻鍔犲ぇ鐨勶紝绺芥湁涓澶╋紝鎴戝戞渻鎶婂畠鍊戝壏骞炽傘 +鏅哄彑鑱斤锛岀劇瑭卞彲瑾 +饧嗗北鐨勫畧璀风琚剼鍏殑鍫呮瘏绮剧鍤囧掞紝渚挎妸姝や簨濂忕煡澶╁笣銆傚ぉ甯濅僵鏈嶆剼鍏殑绮剧锛屽氨鍛藉叐浣嶁激铴婄鎻光緵浜屽北銆 + +How The Foolish Old Man Moved Mountains +Yugong was a ninety-year-old man who lived at the north of two high mountains, Mount Taihang and Mount Wangwu. +Stretching over a wide expanse of land, the mountains blocked yugong鈥檚 way making it inconvenient for him and his family to get around. +One day yugong gathered his family together and said,鈥滾et鈥檚 do our best to level these two mountains. We shall open a road that leads to Yuzhou. What do you think?鈥 +All but his wife agreed with him. +鈥淵ou don鈥檛 have the strength to cut even a small mound,鈥 muttered his wife. 鈥淗ow on earth do you suppose you can level Mount Taihang and Mount Wanwu? Moreover, where will all the earth and rubble go?鈥 +鈥淒ump them into the Sea of Bohai!鈥 said everyone. +So Yugong, his sons, and his grandsons started to break up rocks and remove the earth. They transported the earth and rubble to the Sea of Bohai. +Now Yugong鈥檚 neighbour was a widow who had an only child eight years old. Even the young boy offered his help eagerly. +Summer went by and winter came. It took Yugong and his crew a full year to travel back and forth once. +On the bank of the Yellow River dwelled an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed Yugong saying,鈥滱ren鈥檛 you foolish, my friend? You are very old now, and with whatever remains of your waning strength, you won鈥檛 be able to remove even a corner of the mountain.鈥 +Yugong uttered a sigh and said,鈥滱 biased person like you will never understand. You can鈥檛 even compare with the widow鈥檚 little boy!鈥 +鈥淓ven if I were dead, there will still be my children, my grandchildren, my great grandchildren, my great great grandchildren. They descendants will go on forever. But these mountains will not grow any taler. We shall level them one day!鈥 he declared with confidence. +The wise old man was totally silenced. +When the guardian gods of the mountains saw how determined Yugong and his crew were, they were struck with fear and reported the incident to the Emperor of Heavens. +Filled with admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away. +''' + +result = stats_word.stats_text(text) +print('涓嫳鏂囧瓧绗︾粺璁℃儏鍐靛涓嬶細', result) diff --git a/exercises/1901100039/d07/mymodule/stats_word.py b/exercises/1901100039/d07/mymodule/stats_word.py new file mode 100644 index 000000000..3401243d2 --- /dev/null +++ b/exercises/1901100039/d07/mymodule/stats_word.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Mon Jul 15 15:31:31 2019 + +@author: mtattoo +""" + +# 灏佽缁熻鑻辨枃鍗曡瘝璇嶉鐨勫嚱鏁 +def stats_text_en(text): + # 缁熻杈撳叆鑻扁絺鍗曡瘝鍑虹幇鐨勬鏁帮紝鏈鍚庤繑鍥炩紑涓寜璇嶉闄嶅簭鎺掑垪铴滅殑鏁扮粍 + elements = text.split() # 鎷嗗垎鎵鏈夊崟璇 + words = [] + symbols = ',.*-!' + for element in elements: + for symbol in symbols: + element = element.replace(symbol,'') # 鍓旈櫎瀛楃 + # 鐢╯tr 绫诲瀷鐨 isascii 鏂规硶鍒ゆ柇鏄惁鑻辨枃鍗曡瘝 + if len(element) and element.isascii(): + words.append(element) + counter = {} + word_set = set(words) + + for word in word_set: + counter[word] = words.count(word) + return sorted(counter.items(),key = lambda x: x[1],reverse = True) + +# 灏佽缁熻涓枃姹夊瓧娆℃暟鐨勫嚱鏁 +def stats_text_cn(text): + cn_characters = [] + for character in text: + # 浠nicode涓腑鏂囧瓧绗﹀瓨鍌ㄧ殑鑼冨洿 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + counter = {} + cn_character_set = set(cn_characters) + for character in cn_character_set: + counter[character] = cn_characters.count(character) + return sorted(counter.items(),key = lambda x: x[1],reverse = True) + + +def stats_text( text ): + ''' + 鍚堝苟鏄剧ず涓枃鍙婅嫳鏂囧瓧绗︽暟閲 + ''' + return stats_text_en(text) + stats_text_cn(text) \ No newline at end of file From b7c8d4d957918980f3d5349e384f199e71b4576c Mon Sep 17 00:00:00 2001 From: mTattoo Date: Fri, 19 Jul 2019 14:01:57 +0800 Subject: [PATCH 8/8] Day08 --- exercises/1901100039/d08/main.py | 37 +++++++++++++ .../1901100039/d08/mymodule/stats_word.py | 55 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 exercises/1901100039/d08/main.py create mode 100644 exercises/1901100039/d08/mymodule/stats_word.py diff --git a/exercises/1901100039/d08/main.py b/exercises/1901100039/d08/main.py new file mode 100644 index 000000000..6258ffbdd --- /dev/null +++ b/exercises/1901100039/d08/main.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Wed Jul 19 11:06:36 2019 + +@author: mtattoo +""" + +from mymodule import stats_word +import traceback +import logging + +text = int(input('璇疯緭鍏ユ枃鏈: ')) + +logger = logging.getLogger(__name__) + +def test_traceback(): + try: + stats_word.stats_text(text) + except Exception as errors: + print('test_traceback :', errors) + print(traceback.format_exc()) + +def test_logger(): + try: + stats_word.stats_text(text) + except Exception as errors: + print('test_logger :', errors) + logger.exception(errors) + +if __name__ == "__main__": + stats_word.stats_text(text) + test_traceback() + test_logger() + +result = stats_word.stats_text(text) +print('涓嫳鏂囧瓧绗︾粺璁℃儏鍐靛涓嬶細', result) diff --git a/exercises/1901100039/d08/mymodule/stats_word.py b/exercises/1901100039/d08/mymodule/stats_word.py new file mode 100644 index 000000000..076df5615 --- /dev/null +++ b/exercises/1901100039/d08/mymodule/stats_word.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Mon Jul 15 15:31:31 2019 + +@author: mtattoo +""" + +# 灏佽缁熻鑻辨枃鍗曡瘝璇嶉鐨勫嚱鏁 +def stats_text_en(text): + # 妫鏌ext鐨勭被鍨嬶紝鑻ヨ緭鍏ュ瓧绗︿笉鏄瓧绗︿覆锛屽垯杩斿洖valueError閿欒銆 + if not isinstance(text, str): + raise ValueError('鍙傛暟蹇呴』鏄 str 绫诲瀷锛岃緭鍏ョ被鍨 %s' % type(text)) + # 缁熻杈撳叆鑻扁絺鍗曡瘝鍑虹幇鐨勬鏁帮紝鏈鍚庤繑鍥炩紑涓寜璇嶉闄嶅簭鎺掑垪铴滅殑鏁扮粍 + elements = text.split() # 鎷嗗垎鎵鏈夊崟璇 + words = [] + symbols = ',.*-!' + for element in elements: + for symbol in symbols: + element = element.replace(symbol,'') # 鍓旈櫎瀛楃 + # 鐢╯tr 绫诲瀷鐨 isascii 鏂规硶鍒ゆ柇鏄惁鑻辨枃鍗曡瘝 + if len(element) and element.isascii(): + words.append(element) + counter = {} + word_set = set(words) + + for word in word_set: + counter[word] = words.count(word) + return sorted(counter.items(),key = lambda x: x[1],reverse = True) + +# 灏佽缁熻涓枃姹夊瓧娆℃暟鐨勫嚱鏁 +def stats_text_cn(text): + # 妫鏌ext鐨勭被鍨嬶紝鑻ヨ緭鍏ュ瓧绗︿笉鏄瓧绗︿覆锛屽垯杩斿洖valueError閿欒銆 + if not isinstance(text, str): + raise ValueError('鍙傛暟蹇呴』鏄 str 绫诲瀷锛岃緭鍏ョ被鍨 %s' % type(text)) # 杩斿洖瀹屾暣鐨勯敊璇彁绀轰俊鎭 + cn_characters = [] + for character in text: + # 浠nicode涓腑鏂囧瓧绗﹀瓨鍌ㄧ殑鑼冨洿 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + counter = {} + cn_character_set = set(cn_characters) + for character in cn_character_set: + counter[character] = cn_characters.count(character) + return sorted(counter.items(),key = lambda x: x[1],reverse = True) + + +def stats_text(text): + ''' + 鍚堝苟鏄剧ず涓枃鍙婅嫳鏂囧瓧绗︽暟閲 + ''' + # 妫鏌ext鐨勭被鍨嬶紝鑻ヨ緭鍏ュ瓧绗︿笉鏄瓧绗︿覆锛屽垯杩斿洖valueError閿欒銆 + if not isinstance(text, str): + raise ValueError('鍙傛暟蹇呴』鏄 str 绫诲瀷锛岃緭鍏ョ被鍨 %s' % type(text)) # 杩斿洖瀹屾暣鐨勯敊璇彁绀轰俊鎭 + return stats_text_en(text) + stats_text_cn(text) \ No newline at end of file