diff --git a/exercises/1901100140/1001S02E06_stats_word.py b/exercises/1901100140/1001S02E06_stats_word.py index 040c31a45..0b9fda6c9 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: @@ -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) diff --git a/exercises/1901100140/d07/main.py b/exercises/1901100140/d07/main.py index ef78f4459..31da49457 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,8 @@ 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) - - diff --git a/exercises/1901100140/d07/mymodule/stats_word.py b/exercises/1901100140/d07/mymodule/stats_word.py index 7fda5a1ad..3cf4ec6e5 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,6 @@ def stats_text(text): if __name__=="__main__": result=stats_text(text) -print("合并的英文词频和中文词频==>\n",result) + print("合并的英文词频和中文词频==>\n",result) + diff --git a/exercises/1901100140/d08/main.py b/exercises/1901100140/d08/main.py new file mode 100644 index 000000000..5e7c8fdf5 --- /dev/null +++ b/exercises/1901100140/d08/main.py @@ -0,0 +1,81 @@ +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. +""" + +from mymodule import stats_word +import traceback +import logging + +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 + + diff --git a/exercises/1901100140/d08/mymodule/stats_word.py b/exercises/1901100140/d08/mymodule/stats_word.py new file mode 100644 index 000000000..25ef9f35e --- /dev/null +++ b/exercises/1901100140/d08/mymodule/stats_word.py @@ -0,0 +1,89 @@ +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): + if not isinstance(en_text,str): + raise ValueError("参数必须是str类型,输入类型%s" % type(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): + if not isinstance(cn_text,str): + raise ValueError("参数必须是str类型,输入类型%s" % type(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): + if not isinstance(text,str): + raise ValueError("参数必须是str类型,输入类型%s" % type(text)) + #合并英文词频和中文词频的结果 + return stats_text_en(en_text) + stats_text_cn(cn_text) + +if __name__=="__main__": + result=stats_text(text) + print("合并的英文词频和中文词频==>\n",result) +