From abea8b5004dfed9c3bdd9713993fcc278e9d9690 Mon Sep 17 00:00:00 2001 From: taoyajun <53133575+taoyajun@users.noreply.github.com> Date: Sun, 8 Sep 2019 11:50:57 +0800 Subject: [PATCH 01/24] Create 1001S02E05_string.py --- exercises/1901100140/1001S02E05_string.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100140/1001S02E05_string.py diff --git a/exercises/1901100140/1001S02E05_string.py b/exercises/1901100140/1001S02E05_string.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/exercises/1901100140/1001S02E05_string.py @@ -0,0 +1 @@ + From 59cb0a20092dcaf9727cf30fc18034aac679b1d0 Mon Sep 17 00:00:00 2001 From: taoyajun <53133575+taoyajun@users.noreply.github.com> Date: Sun, 8 Sep 2019 11:53:08 +0800 Subject: [PATCH 02/24] Create 1001S02E05_stats_text.py --- exercises/1901100140/1001S02E05_stats_text.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100140/1001S02E05_stats_text.py diff --git a/exercises/1901100140/1001S02E05_stats_text.py b/exercises/1901100140/1001S02E05_stats_text.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/exercises/1901100140/1001S02E05_stats_text.py @@ -0,0 +1 @@ + From f9c248198b1fb9a9a1e464b275cb5377c3ad2ce7 Mon Sep 17 00:00:00 2001 From: taoyajun <53133575+taoyajun@users.noreply.github.com> Date: Sun, 8 Sep 2019 11:53:56 +0800 Subject: [PATCH 03/24] Create 1001S02E05_array.py --- exercises/1901100140/1001S02E05_array.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100140/1001S02E05_array.py diff --git a/exercises/1901100140/1001S02E05_array.py b/exercises/1901100140/1001S02E05_array.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/exercises/1901100140/1001S02E05_array.py @@ -0,0 +1 @@ + From 4e98215b2d865e7aeb2293ee2c2debc303631aa0 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Tue, 24 Sep 2019 21:09:51 +0800 Subject: [PATCH 04/24] Update 1001S02E05_array.py --- exercises/1901100140/1001S02E05_array.py | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/exercises/1901100140/1001S02E05_array.py b/exercises/1901100140/1001S02E05_array.py index 8b1378917..6ea94cce4 100644 --- a/exercises/1901100140/1001S02E05_array.py +++ b/exercises/1901100140/1001S02E05_array.py @@ -1 +1,26 @@ +[0,1,2,3,4,5,6,7,8,9] +sample_list=[0,1,2,3,4,5,6,7,8,9] +reverse_list=sample_list[::-1] +print(reverse_list) + +#将翻转后的数组拼接成字符 +joined_str="".join(str(i) for i in reverse_list) +print(joined_str) + +#用字符切片的方式取出第三到第八个字符 +sliced_str=joined_str[2:8] +print(sliced_str) + +#将获得的字符串进行反转 +reverse_str=sliced_str[::-1] +print(reverse_str) + +#将结果转换为int类型 +int_value=int(reverse_str) +print(int_value) + +#分别转换成二进制、八进制、十六进制 +print(bin(int_value)) +print(oct(int_value)) +print(hex(int_value)) From 4274044b5b9a9616cd56a3495f1cffb593e99318 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Tue, 24 Sep 2019 21:10:08 +0800 Subject: [PATCH 05/24] Update 1001S02E05_stats_text.py --- exercises/1901100140/1001S02E05_stats_text.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/exercises/1901100140/1001S02E05_stats_text.py b/exercises/1901100140/1001S02E05_stats_text.py index 8b1378917..a7271e986 100644 --- a/exercises/1901100140/1001S02E05_stats_text.py +++ b/exercises/1901100140/1001S02E05_stats_text.py @@ -1 +1,42 @@ +sample_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 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 east to explain,it may be a good idea. +Namespaces are one honking great idea-- let's do more of those! +""" + +elements=sample_text.split() +words=[] +symbols=",.*-" +for element in elements: + for symbol in symbols: + element=element.replace("symbols","") + if len(element): + words.append(element) +print("打印正常的英文单词",words) + +# 统计单词出现的次数 +counter={} +word_set=set(words) +for word in word_set: + counter[word]=words.count(word) +print("统计单词出现的次数",counter) +#按照出现次数从大到小统计所有的单词及出现的次数 +print("按照出现次数从大到小统计所有的单词及出现的次数",sorted(counter.items(),key=lambda x:x[1],reverse=True)) From 5d0a35b8ff462744d36eefcf4ddc4461c290fd47 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Tue, 24 Sep 2019 21:10:15 +0800 Subject: [PATCH 06/24] Update 1001S02E05_string.py --- exercises/1901100140/1001S02E05_string.py | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/exercises/1901100140/1001S02E05_string.py b/exercises/1901100140/1001S02E05_string.py index 8b1378917..f9ea576d8 100644 --- a/exercises/1901100140/1001S02E05_string.py +++ b/exercises/1901100140/1001S02E05_string.py @@ -1 +1,38 @@ +sample_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 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 east to explain,it may be a good idea. +Namespaces are one honking great idea-- let's do more of those! +""" +# 1 将字符串样本里的“better”全部替换成“worse” +text=sample_text.replace("better","worse") +print("将字符串样本text里的better全部替换成worse",text) + # 2 将单词中包含ea的单词剔除 +words=text.split() +filtered=[] +for word in words: + if word.find("ea")<0: + filtered.append(word) +print("将单词中包含ea的单词剔除",filtered) +#3 将单词中的字母进行大小写翻转 +swapcased=[i.swapcase()for i in filtered] +print("字母大小写翻转后的结果",swapcased) +#4 将所有单词按a-z升序排列 +print("将所有单词按z-a降序排列",sorted(swapcased)) From e713268f4bee07bd5398cf0d1beef7a77ff322d7 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Tue, 24 Sep 2019 21:10:23 +0800 Subject: [PATCH 07/24] Update 1010S02E03_calculator.py --- exercises/1901100140/1010S02E03_calculator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/1901100140/1010S02E03_calculator.py b/exercises/1901100140/1010S02E03_calculator.py index b963d3cdd..a81353434 100644 --- a/exercises/1901100140/1010S02E03_calculator.py +++ b/exercises/1901100140/1010S02E03_calculator.py @@ -16,3 +16,4 @@ print(a,"/",b,"=",a/b) else: print("无效的运算符") + From 71733aa5893d438c7c8508619b012ec9444c9754 Mon Sep 17 00:00:00 2001 From: taoyajun <53133575+taoyajun@users.noreply.github.com> Date: Fri, 8 Nov 2019 22:37:27 +0800 Subject: [PATCH 08/24] Create 1001S02E06_stats_word.py --- exercises/1901100140/1001S02E06_stats_word.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100140/1001S02E06_stats_word.py diff --git a/exercises/1901100140/1001S02E06_stats_word.py b/exercises/1901100140/1001S02E06_stats_word.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/exercises/1901100140/1001S02E06_stats_word.py @@ -0,0 +1 @@ + From ccb699d91e6524726eef01945d56b98cae8d0a27 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Mon, 11 Nov 2019 16:41:19 +0800 Subject: [PATCH 09/24] Update 1001S02E06_stats_word.py --- exercises/1901100140/1001S02E06_stats_word.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/exercises/1901100140/1001S02E06_stats_word.py b/exercises/1901100140/1001S02E06_stats_word.py index 8b1378917..91192d8bd 100644 --- a/exercises/1901100140/1001S02E06_stats_word.py +++ b/exercises/1901100140/1001S02E06_stats_word.py @@ -1 +1,73 @@ +en_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 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 east to explain,it may be a good idea. +Namespaces are one honking great idea-- let's do more of those! +""" +def stats_text_en(text): + elements=en_text.split() + words=[] + symbols=",.*-" + for element in elements: + for symbol in symbols: + element=element.replace("symbols","") + if len(element): + 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) + + +cn_text=""" +优美胜于丑陋 +明了胜于晦涩 +简洁胜于复杂 +复杂胜于凌乱 +扁平胜于嵌套 +间隔胜于紧凑 +可读性很重要 +即使假借特例的实用之名,也不可违背这些规则 +不要包容所有错误,除非你确定需要这样做 +当存在多种可能,不要尝试去猜测 +而是尽量找一种,最好是唯一一种明显的解决方案 +虽然这并不容易,因为你不是 Python 之父 +做也许好过不做,但不假思索就动手还不如不做 +*** +""" + +def stats_text_cn(text): + cn_characters=[] + for character in text: + 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) + +if __name__=="__main__": + en_result=stats_text_en(en_text) + cn_result=stats_text_cn(cn_text) +print("统计参数中英文单词出现的次料==》\n",en_result) +print("统计参数中中文单词出现的次数==》\n",cn_result) From 52439d532f85035bcd5920140e051824cc5c1cbe Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sun, 17 Nov 2019 09:56:25 +0800 Subject: [PATCH 10/24] Create stats_word.py --- .../1901100140/d07/mymodule/stats_word.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 exercises/1901100140/d07/mymodule/stats_word.py diff --git a/exercises/1901100140/d07/mymodule/stats_word.py b/exercises/1901100140/d07/mymodule/stats_word.py new file mode 100644 index 000000000..91192d8bd --- /dev/null +++ b/exercises/1901100140/d07/mymodule/stats_word.py @@ -0,0 +1,73 @@ +en_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 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 east to explain,it may be a good idea. +Namespaces are one honking great idea-- let's do more of those! +""" + +def stats_text_en(text): + elements=en_text.split() + words=[] + symbols=",.*-" + for element in elements: + for symbol in symbols: + element=element.replace("symbols","") + if len(element): + 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) + + +cn_text=""" +优美胜于丑陋 +明了胜于晦涩 +简洁胜于复杂 +复杂胜于凌乱 +扁平胜于嵌套 +间隔胜于紧凑 +可读性很重要 +即使假借特例的实用之名,也不可违背这些规则 +不要包容所有错误,除非你确定需要这样做 +当存在多种可能,不要尝试去猜测 +而是尽量找一种,最好是唯一一种明显的解决方案 +虽然这并不容易,因为你不是 Python 之父 +做也许好过不做,但不假思索就动手还不如不做 +*** +""" + +def stats_text_cn(text): + cn_characters=[] + for character in text: + 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) + +if __name__=="__main__": + en_result=stats_text_en(en_text) + cn_result=stats_text_cn(cn_text) +print("统计参数中英文单词出现的次料==》\n",en_result) +print("统计参数中中文单词出现的次数==》\n",cn_result) From f82d8c4480c5442666a3e1fbeb8c04e6beab3f54 Mon Sep 17 00:00:00 2001 From: taoyajun <53133575+taoyajun@users.noreply.github.com> Date: Sun, 17 Nov 2019 11:16:43 +0800 Subject: [PATCH 11/24] Create main.py --- exercises/1901100140/d07/main.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100140/d07/main.py diff --git a/exercises/1901100140/d07/main.py b/exercises/1901100140/d07/main.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/exercises/1901100140/d07/main.py @@ -0,0 +1 @@ + From b00ed7361c1327fbca34dbf14c93e0cfe4b1bc2a Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sun, 17 Nov 2019 12:32:58 +0800 Subject: [PATCH 12/24] Update main.py --- exercises/1901100140/d07/main.py | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/exercises/1901100140/d07/main.py b/exercises/1901100140/d07/main.py index 8b1378917..620651b86 100644 --- a/exercises/1901100140/d07/main.py +++ b/exercises/1901100140/d07/main.py @@ -1 +1,56 @@ +text=""" +愚公移山 +太行、王屋二山的背面,住了一个九十岁的老翁,名叫愚公。二山占地广阔,挡住去路,使他和家人往来极为不便。 +一天,愚公召集家人説:“让我们各尽其力,铲平二山,开条道路,直通豫州,你们认为怎样?” +大家都异口同声赞成,只有他的妻子表示怀疑,并説:“你边开凿一个小丘的力量都没有,怎么可能铲平太行、王屋二山呢?况且,凿出的土石又丢到哪里去呢?” + +大家都热烈地説:“把土石丢进渤海里。” +于是愚公就和儿孙,一起开挖土,把土石搬运到渤海里去。 +愚公的邻居是个寡妇,有个儿子八岁也兴致勃勃地走来帮忙。 +寒来暑往,他们要一年才能往返渤海一次。 + +住在黄河河畔的智叟,看见他们这样辛苦,取笑愚公説:“你不是很愚蠢吗?你已一把年纪了,就是用尽你的气力,也不能 +挖去山的一角呢?” +愚公叹息道:“你有这样的成见,是不会明白的。你比那寡妇的小儿子还不如呢!就算我死了,还有我的儿子,我的孙子,我的 +曾孙子,他们一直传下去。而这二山是不会加大的,总有一天,我们会把它们铲平。” +智叟听了,无话可説。 +二山的守护神被愚公的坚毅精神吓倒,便把此事奏知天帝。天帝佩服愚公的精神,就命两位大力神背走二山。 + +How The Foolish Old Man Moved Mountains +Yugong was a ninety-year-old man who lived at the north of two high mountains,mount Taixing and Mount Wangwu. +Stretching over a wide expanse of land,the mountains blocded yugong's way making it inconvenient for him +and his family to get around. +One day yugong gathered his family together and siad,"Let's 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. +"You don't have the strength to cut even a small mound,"muttered his wife."How on earth do you suppose +you can level Mount Taixing and Mount Wangwu?Moreover,Where will all the earth and rubble go?" +"Dump them into the sea of Bohai!"said everone. + +So yugong,his sons,and his grandsons started to break up rocks and remove the earth.They transported +the earth and rubble to the sec of Bohai. + +Now Yugong's neighbour was a widow who had an only child eight years old.Evening 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 on old man much respected for his wisdom.When he saw their +back-breaking labour,he ridiculed Yugong saying,"Are't you foolish,my friend?you are very old now,and +with whatever remains of your waning strength,you won't be able to remove even a corner of the mountain." + +Yugong uttered a sigh and said,"A biased person like you will never understand.You can't even cpmpare +with the widow's little boy!" + +"Even 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 admiratiion for Yugong,the Emperor of Heavens ordered tow mighty gods to carry the mountains awawy. +""" \ No newline at end of file From 6049d3a44e77046809aa5bf895189bae9f421d45 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Mon, 18 Nov 2019 14:37:13 +0800 Subject: [PATCH 13/24] Update 1001S02E06_stats_word.py --- exercises/1901100140/1001S02E06_stats_word.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/exercises/1901100140/1001S02E06_stats_word.py b/exercises/1901100140/1001S02E06_stats_word.py index 91192d8bd..ba4e8897f 100644 --- a/exercises/1901100140/1001S02E06_stats_word.py +++ b/exercises/1901100140/1001S02E06_stats_word.py @@ -21,7 +21,7 @@ Namespaces are one honking great idea-- let's do more of those! """ -def stats_text_en(text): +def stats_text_en(en_text): elements=en_text.split() words=[] symbols=",.*-" @@ -55,9 +55,9 @@ def stats_text_en(text): *** """ -def stats_text_cn(text): +def stats_text_cn(cn_text): cn_characters=[] - for character in text: + for character in cn_text: if "\u4e00"<=character<="\u9fff": cn_characters.append(character) counter={} @@ -71,3 +71,4 @@ def stats_text_cn(text): cn_result=stats_text_cn(cn_text) print("统计参数中英文单词出现的次料==》\n",en_result) print("统计参数中中文单词出现的次数==》\n",cn_result) + From d3640240efe286bfc596f65edf26e95b6e9080dd Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Mon, 18 Nov 2019 14:38:11 +0800 Subject: [PATCH 14/24] Update main.py --- exercises/1901100140/d07/main.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/exercises/1901100140/d07/main.py b/exercises/1901100140/d07/main.py index 620651b86..ef78f4459 100644 --- a/exercises/1901100140/d07/main.py +++ b/exercises/1901100140/d07/main.py @@ -1,4 +1,5 @@ -text=""" +from mymodule import stats_word +sample_text=""" 愚公移山 太行、王屋二山的背面,住了一个九十岁的老翁,名叫愚公。二山占地广阔,挡住去路,使他和家人往来极为不便。 @@ -53,4 +54,10 @@ with fear and reported the incident to the Emperor of Heavens. Filled with admiratiion for Yugong,the Emperor of Heavens ordered tow mighty gods to carry the mountains awawy. -""" \ No newline at end of file +""" + +result=stats_word.stats_text(sample_text) + +print("统计结果",result) + + From eb4381507c13c91f90a97bbf1e223587e6918d64 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Mon, 18 Nov 2019 14:38:19 +0800 Subject: [PATCH 15/24] Update stats_word.py --- .../1901100140/d07/mymodule/stats_word.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/exercises/1901100140/d07/mymodule/stats_word.py b/exercises/1901100140/d07/mymodule/stats_word.py index 91192d8bd..7fda5a1ad 100644 --- a/exercises/1901100140/d07/mymodule/stats_word.py +++ b/exercises/1901100140/d07/mymodule/stats_word.py @@ -21,7 +21,7 @@ Namespaces are one honking great idea-- let's do more of those! """ -def stats_text_en(text): +def stats_text_en(en_text): elements=en_text.split() words=[] symbols=",.*-" @@ -55,9 +55,9 @@ def stats_text_en(text): *** """ -def stats_text_cn(text): +def stats_text_cn(cn_text): cn_characters=[] - for character in text: + for character in cn_text: if "\u4e00"<=character<="\u9fff": cn_characters.append(character) counter={} @@ -69,5 +69,15 @@ def stats_text_cn(text): if __name__=="__main__": en_result=stats_text_en(en_text) cn_result=stats_text_cn(cn_text) -print("统计参数中英文单词出现的次料==》\n",en_result) -print("统计参数中中文单词出现的次数==》\n",cn_result) +print("统计参数中英文单词出现的次料==>\n",en_result) +print("统计参数中中文单词出现的次数==>\n",cn_result) + +text=en_text + cn_text +def stats_text(text): + #合并英文词频和中文词频的结果 + return stats_text_en(en_text) + stats_text_cn(cn_text) + +if __name__=="__main__": + result=stats_text(text) +print("合并的英文词频和中文词频==>\n",result) + From 84fec86088265c7ccc5affe530a2610aa5280906 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Mon, 18 Nov 2019 21:17:02 +0800 Subject: [PATCH 16/24] Create main.py --- exercises/1901100140/d08/main.py | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 exercises/1901100140/d08/main.py diff --git a/exercises/1901100140/d08/main.py b/exercises/1901100140/d08/main.py new file mode 100644 index 000000000..ef78f4459 --- /dev/null +++ b/exercises/1901100140/d08/main.py @@ -0,0 +1,63 @@ +from mymodule import stats_word +sample_text=""" +愚公移山 + +太行、王屋二山的背面,住了一个九十岁的老翁,名叫愚公。二山占地广阔,挡住去路,使他和家人往来极为不便。 +一天,愚公召集家人説:“让我们各尽其力,铲平二山,开条道路,直通豫州,你们认为怎样?” +大家都异口同声赞成,只有他的妻子表示怀疑,并説:“你边开凿一个小丘的力量都没有,怎么可能铲平太行、王屋二山呢?况且,凿出的土石又丢到哪里去呢?” + +大家都热烈地説:“把土石丢进渤海里。” +于是愚公就和儿孙,一起开挖土,把土石搬运到渤海里去。 +愚公的邻居是个寡妇,有个儿子八岁也兴致勃勃地走来帮忙。 +寒来暑往,他们要一年才能往返渤海一次。 + +住在黄河河畔的智叟,看见他们这样辛苦,取笑愚公説:“你不是很愚蠢吗?你已一把年纪了,就是用尽你的气力,也不能 +挖去山的一角呢?” +愚公叹息道:“你有这样的成见,是不会明白的。你比那寡妇的小儿子还不如呢!就算我死了,还有我的儿子,我的孙子,我的 +曾孙子,他们一直传下去。而这二山是不会加大的,总有一天,我们会把它们铲平。” +智叟听了,无话可説。 +二山的守护神被愚公的坚毅精神吓倒,便把此事奏知天帝。天帝佩服愚公的精神,就命两位大力神背走二山。 + +How The Foolish Old Man Moved Mountains +Yugong was a ninety-year-old man who lived at the north of two high mountains,mount Taixing and Mount Wangwu. +Stretching over a wide expanse of land,the mountains blocded yugong's way making it inconvenient for him +and his family to get around. +One day yugong gathered his family together and siad,"Let's 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. +"You don't have the strength to cut even a small mound,"muttered his wife."How on earth do you suppose +you can level Mount Taixing and Mount Wangwu?Moreover,Where will all the earth and rubble go?" +"Dump them into the sea of Bohai!"said everone. + +So yugong,his sons,and his grandsons started to break up rocks and remove the earth.They transported +the earth and rubble to the sec of Bohai. + +Now Yugong's neighbour was a widow who had an only child eight years old.Evening 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 on old man much respected for his wisdom.When he saw their +back-breaking labour,he ridiculed Yugong saying,"Are't you foolish,my friend?you are very old now,and +with whatever remains of your waning strength,you won't be able to remove even a corner of the mountain." + +Yugong uttered a sigh and said,"A biased person like you will never understand.You can't even cpmpare +with the widow's little boy!" + +"Even 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 admiratiion for Yugong,the Emperor of Heavens ordered tow mighty gods to carry the mountains awawy. +""" + +result=stats_word.stats_text(sample_text) + +print("统计结果",result) + + From b29510ab4ba65ae0577ff406c59537428de0b742 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Mon, 18 Nov 2019 21:17:14 +0800 Subject: [PATCH 17/24] Create stats_word.py --- .../1901100140/d08/mymodule/stats_word.py | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 exercises/1901100140/d08/mymodule/stats_word.py diff --git a/exercises/1901100140/d08/mymodule/stats_word.py b/exercises/1901100140/d08/mymodule/stats_word.py new file mode 100644 index 000000000..7fda5a1ad --- /dev/null +++ b/exercises/1901100140/d08/mymodule/stats_word.py @@ -0,0 +1,83 @@ +en_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 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 east to explain,it may be a good idea. +Namespaces are one honking great idea-- let's do more of those! +""" + +def stats_text_en(en_text): + elements=en_text.split() + words=[] + symbols=",.*-" + for element in elements: + for symbol in symbols: + element=element.replace("symbols","") + if len(element): + 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) + + +cn_text=""" +优美胜于丑陋 +明了胜于晦涩 +简洁胜于复杂 +复杂胜于凌乱 +扁平胜于嵌套 +间隔胜于紧凑 +可读性很重要 +即使假借特例的实用之名,也不可违背这些规则 +不要包容所有错误,除非你确定需要这样做 +当存在多种可能,不要尝试去猜测 +而是尽量找一种,最好是唯一一种明显的解决方案 +虽然这并不容易,因为你不是 Python 之父 +做也许好过不做,但不假思索就动手还不如不做 +*** +""" + +def stats_text_cn(cn_text): + cn_characters=[] + for character in cn_text: + 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) + +if __name__=="__main__": + en_result=stats_text_en(en_text) + cn_result=stats_text_cn(cn_text) +print("统计参数中英文单词出现的次料==>\n",en_result) +print("统计参数中中文单词出现的次数==>\n",cn_result) + +text=en_text + cn_text +def stats_text(text): + #合并英文词频和中文词频的结果 + return stats_text_en(en_text) + stats_text_cn(cn_text) + +if __name__=="__main__": + result=stats_text(text) +print("合并的英文词频和中文词频==>\n",result) + From 4eac0cf5081ff3078e7b51465a52bfe44e01c516 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Fri, 22 Nov 2019 22:32:58 +0800 Subject: [PATCH 18/24] Update stats_word.py --- exercises/1901100140/d08/mymodule/stats_word.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/exercises/1901100140/d08/mymodule/stats_word.py b/exercises/1901100140/d08/mymodule/stats_word.py index 7fda5a1ad..2dbcf3002 100644 --- a/exercises/1901100140/d08/mymodule/stats_word.py +++ b/exercises/1901100140/d08/mymodule/stats_word.py @@ -22,6 +22,8 @@ """ def stats_text_en(en_text): + if not isinstance(en_text,str): + raise ValueError("参数必须是str类型,输入类型%s" % type(en_text)) elements=en_text.split() words=[] symbols=",.*-" @@ -56,6 +58,8 @@ def stats_text_en(en_text): """ def stats_text_cn(cn_text): + if not isinstance(cn_text,str): + raise ValueError("参数必须是str类型,输入类型%s" % type(cn_text)) cn_characters=[] for character in cn_text: if "\u4e00"<=character<="\u9fff": @@ -74,6 +78,8 @@ def stats_text_cn(cn_text): text=en_text + cn_text def stats_text(text): + if not isinstance(text,str): + raise ValueError("参数必须是str类型,输入类型%s" % type(text)) #合并英文词频和中文词频的结果 return stats_text_en(en_text) + stats_text_cn(cn_text) From 5dec9843933b3fcb64bbe28beb9089d6ecd84b17 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sat, 23 Nov 2019 15:13:51 +0800 Subject: [PATCH 19/24] Update 1001S02E06_stats_word.py --- exercises/1901100140/1001S02E06_stats_word.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/1901100140/1001S02E06_stats_word.py b/exercises/1901100140/1001S02E06_stats_word.py index 040c31a45..f5576ea59 100644 --- a/exercises/1901100140/1001S02E06_stats_word.py +++ b/exercises/1901100140/1001S02E06_stats_word.py @@ -21,8 +21,8 @@ Namespaces are one honking great idea-- let's do more of those! """ -def stats_text_en(en_text): - elements=en_text.split() +def stats_text_en(text): + elements=text.split() words=[] symbols=",.*-" for element in elements: @@ -55,9 +55,9 @@ def stats_text_en(en_text): *** """ -def stats_text_cn(cn_text): +def stats_text_cn(text): cn_characters=[] - for character in cn_text: + for character in text: if "\u4e00"<=character<="\u9fff": cn_characters.append(character) counter={} @@ -69,5 +69,5 @@ def stats_text_cn(cn_text): if __name__=="__main__": en_result=stats_text_en(en_text) cn_result=stats_text_cn(cn_text) -print("统计参数中英文单词出现的次料==》\n",en_result) -print("统计参数中中文单词出现的次数==》\n",cn_result) + print("统计参数中英文单词出现的次料==》\n",en_result) + print("统计参数中中文单词出现的次数==》\n",cn_result) From 06a6888e278d0fdb36cdc13e5796f9917d0d992d Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sat, 23 Nov 2019 15:13:56 +0800 Subject: [PATCH 20/24] Update main.py --- exercises/1901100140/d07/main.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/exercises/1901100140/d07/main.py b/exercises/1901100140/d07/main.py index ef78f4459..ddbb44162 100644 --- a/exercises/1901100140/d07/main.py +++ b/exercises/1901100140/d07/main.py @@ -1,5 +1,4 @@ -from mymodule import stats_word -sample_text=""" +text=""" 愚公移山 太行、王屋二山的背面,住了一个九十岁的老翁,名叫愚公。二山占地广阔,挡住去路,使他和家人往来极为不便。 @@ -55,9 +54,9 @@ Filled with admiratiion for Yugong,the Emperor of Heavens ordered tow mighty gods to carry the mountains awawy. """ +from mymodule import stats_word -result=stats_word.stats_text(sample_text) +result=stats_word.stats_text(text) print("统计结果",result) - From 08b1015860e6f61330f7ef5dd5f268190020776f Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sat, 23 Nov 2019 15:14:03 +0800 Subject: [PATCH 21/24] Update stats_word.py --- exercises/1901100140/d07/mymodule/stats_word.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/1901100140/d07/mymodule/stats_word.py b/exercises/1901100140/d07/mymodule/stats_word.py index 7fda5a1ad..6691a22e5 100644 --- a/exercises/1901100140/d07/mymodule/stats_word.py +++ b/exercises/1901100140/d07/mymodule/stats_word.py @@ -21,8 +21,8 @@ Namespaces are one honking great idea-- let's do more of those! """ -def stats_text_en(en_text): - elements=en_text.split() +def stats_text_en(text): + elements=text.split() words=[] symbols=",.*-" for element in elements: @@ -55,9 +55,9 @@ def stats_text_en(en_text): *** """ -def stats_text_cn(cn_text): +def stats_text_cn(text): cn_characters=[] - for character in cn_text: + for character in text: if "\u4e00"<=character<="\u9fff": cn_characters.append(character) counter={} @@ -69,8 +69,8 @@ def stats_text_cn(cn_text): if __name__=="__main__": en_result=stats_text_en(en_text) cn_result=stats_text_cn(cn_text) -print("统计参数中英文单词出现的次料==>\n",en_result) -print("统计参数中中文单词出现的次数==>\n",cn_result) + print("统计参数中英文单词出现的次料==>\n",en_result) + print("统计参数中中文单词出现的次数==>\n",cn_result) text=en_text + cn_text def stats_text(text): @@ -79,5 +79,5 @@ def stats_text(text): if __name__=="__main__": result=stats_text(text) -print("合并的英文词频和中文词频==>\n",result) + print("合并的英文词频和中文词频==>\n",result) From 994a943bcd970c603e351918892707545b7aaea8 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sat, 23 Nov 2019 15:14:09 +0800 Subject: [PATCH 22/24] Update main.py --- exercises/1901100140/d08/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/1901100140/d08/main.py b/exercises/1901100140/d08/main.py index ef78f4459..3f02b7f26 100644 --- a/exercises/1901100140/d08/main.py +++ b/exercises/1901100140/d08/main.py @@ -1,4 +1,3 @@ -from mymodule import stats_word sample_text=""" 愚公移山 @@ -56,8 +55,9 @@ Filled with admiratiion for Yugong,the Emperor of Heavens ordered tow mighty gods to carry the mountains awawy. """ +from mymodule import stats_word + result=stats_word.stats_text(sample_text) print("统计结果",result) - From 59fbd0c48dd699550b4b8a4d2badaf62e0fb65f0 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sat, 23 Nov 2019 15:14:14 +0800 Subject: [PATCH 23/24] Update stats_word.py --- exercises/1901100140/d08/mymodule/stats_word.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/1901100140/d08/mymodule/stats_word.py b/exercises/1901100140/d08/mymodule/stats_word.py index 2dbcf3002..25ef9f35e 100644 --- a/exercises/1901100140/d08/mymodule/stats_word.py +++ b/exercises/1901100140/d08/mymodule/stats_word.py @@ -73,8 +73,8 @@ def stats_text_cn(cn_text): if __name__=="__main__": en_result=stats_text_en(en_text) cn_result=stats_text_cn(cn_text) -print("统计参数中英文单词出现的次料==>\n",en_result) -print("统计参数中中文单词出现的次数==>\n",cn_result) + print("统计参数中英文单词出现的次数==>\n",en_result) + print("统计参数中中文单词出现的次数==>\n",cn_result) text=en_text + cn_text def stats_text(text): @@ -85,5 +85,5 @@ def stats_text(text): if __name__=="__main__": result=stats_text(text) -print("合并的英文词频和中文词频==>\n",result) + print("合并的英文词频和中文词频==>\n",result) From caea5f05d6c681b8803ffc944aa2b00087390ae9 Mon Sep 17 00:00:00 2001 From: taoyajun <972434938@qq.com> Date: Sat, 23 Nov 2019 15:37:44 +0800 Subject: [PATCH 24/24] Update main.py --- exercises/1901100140/d08/main.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/exercises/1901100140/d08/main.py b/exercises/1901100140/d08/main.py index 3f02b7f26..5e7c8fdf5 100644 --- a/exercises/1901100140/d08/main.py +++ b/exercises/1901100140/d08/main.py @@ -56,8 +56,26 @@ """ from mymodule import stats_word +import traceback +import logging -result=stats_word.stats_text(sample_text) +logger=logging.getLogger(__name__) + +def test_traceback(): + try: + stats_word.stats_text(1) + except Exception as e: + print("test_traceback=>",e) + print(traceback.format_exc()) + +def test_logger(): + try: + stats_word.stats_text(1) + except Exception as e: + logger.exception(e) + +if __name__=="__main__": + test_traceback + test_logger -print("统计结果",result)