From 190060ec087557f1fa21f11741730f7f94ac046d Mon Sep 17 00:00:00 2001 From: huangmengzhu <654823607@qq.com> Date: Thu, 7 Nov 2019 22:55:01 +0800 Subject: [PATCH 1/4] 1001S02E05 1001S02E05 --- .../1901100209/1001S02E05/1001S02E05_array.py | 17 ++++++++ .../1001S02E05/1001S02E05_stats_text.py | 39 +++++++++++++++++++ .../1001S02E05/1001S02E05_string.py | 38 ++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 exercises/1901100209/1001S02E05/1001S02E05_array.py create mode 100644 exercises/1901100209/1001S02E05/1001S02E05_stats_text.py create mode 100644 exercises/1901100209/1001S02E05/1001S02E05_string.py diff --git a/exercises/1901100209/1001S02E05/1001S02E05_array.py b/exercises/1901100209/1001S02E05/1001S02E05_array.py new file mode 100644 index 000000000..55429d955 --- /dev/null +++ b/exercises/1901100209/1001S02E05/1001S02E05_array.py @@ -0,0 +1,17 @@ +text=[0,1,3,4,5,6,7,8,9] +text.reverse() +print("数字反转",text) + +joined_str="".join([str(i) for i in text]) +print("拼接成字符串",joined_str) + +slice_ed=joined_str[2:8] +print("切片",slice_ed) + +reverse_str=slice_ed[::-1] +print("反转",reverse_str) +slice_int=int(reverse_str) +print("转换为int类型",slice_int) +print("转换为二进制",bin(slice_int)) +print("转换为八进制",oct(slice_int)) +print("转换为十六进制",hex(slice_int)) diff --git a/exercises/1901100209/1001S02E05/1001S02E05_stats_text.py b/exercises/1901100209/1001S02E05/1001S02E05_stats_text.py new file mode 100644 index 000000000..735443d13 --- /dev/null +++ b/exercises/1901100209/1001S02E05/1001S02E05_stats_text.py @@ -0,0 +1,39 @@ +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! +''' +elements=text.split() +words=[] +symbols=",.*-!" +for element in elements: + for symbol in symbols: + element=element.replace(symbol,"") + 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)) diff --git a/exercises/1901100209/1001S02E05/1001S02E05_string.py b/exercises/1901100209/1001S02E05/1001S02E05_string.py new file mode 100644 index 000000000..165c54636 --- /dev/null +++ b/exercises/1901100209/1001S02E05/1001S02E05_string.py @@ -0,0 +1,38 @@ +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! +''' +text=text.replace('better','worse') +print(text) +text1=text.split() +word="ea" +worde=[] +for i in text1: + if i.find(word)>-1: + del i + else: + worde.append(i) +print("将ea删除",worde) +turned=[i.swapcase() for i in worde] +print("大小写翻转",turned) +print("升序排序",sorted(turned)) \ No newline at end of file From 2efd930c44578539e00b4aa8c338a35df6285917 Mon Sep 17 00:00:00 2001 From: huangmengzhu <654823607@qq.com> Date: Tue, 12 Nov 2019 23:07:38 +0800 Subject: [PATCH 2/4] Create 1001S02E06_stats_word.py --- .../1001S02E06/1001S02E06_stats_word.py | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 exercises/1901100209/1001S02E06/1001S02E06_stats_word.py diff --git a/exercises/1901100209/1001S02E06/1001S02E06_stats_word.py b/exercises/1901100209/1001S02E06/1001S02E06_stats_word.py new file mode 100644 index 000000000..de1422a1e --- /dev/null +++ b/exercises/1901100209/1001S02E06/1001S02E06_stats_word.py @@ -0,0 +1,64 @@ +def stats_text_en(text): + """统计参数中英文单词出现的次数,并按降序排列数组""" + elements=text.split() + words=[] + symbols=",.*-!" + for element in elements: + for symbol in symbols: + element=element.replace(symbol,"") + if len(element): + words.append(element) + print("正常英文单词",words) + 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_courter=[] + for courter in text: + if '\u4e00'<= courter <='\u9fff': + cn_courter.append(courter) + counter_text={} + for i in set(cn_courter): + counter_text[i]=cn_courter.count(i) + return sorted(counter_text.items(),key=lambda x: x[1],reverse=True) + +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 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! +''' +cn_text=""" +python学习 +好好学习 +天天向上 +""" +first=stats_text_en(en_text) +print(first) +second=stats_text_cn(cn_text) +print(second) + + + From 890fd23ec8ee85077c988f3ee74661646324fc38 Mon Sep 17 00:00:00 2001 From: huangmengzhu <654823607@qq.com> Date: Sat, 16 Nov 2019 15:29:42 +0800 Subject: [PATCH 3/4] 007 007 --- exercises/1901100209/d07/main.py | 12 ++++ .../1901100209/d07/mymodule/stats_word.py | 68 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 exercises/1901100209/d07/main.py create mode 100644 exercises/1901100209/d07/mymodule/stats_word.py diff --git a/exercises/1901100209/d07/main.py b/exercises/1901100209/d07/main.py new file mode 100644 index 000000000..90417ee7b --- /dev/null +++ b/exercises/1901100209/d07/main.py @@ -0,0 +1,12 @@ +from mymodule import stats_word +smaple_text=""" +好好学习,天天向上,努力工作 +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.""" + +result=stats_word.stats_text(smaple_text) +print(result) \ No newline at end of file diff --git a/exercises/1901100209/d07/mymodule/stats_word.py b/exercises/1901100209/d07/mymodule/stats_word.py new file mode 100644 index 000000000..385623a74 --- /dev/null +++ b/exercises/1901100209/d07/mymodule/stats_word.py @@ -0,0 +1,68 @@ +def stats_text_en(text): + """统计参数中英文单词出现的次数,并按降序排列数组""" + elements=text.split() + words=[] + symbols=",.*-!" + for element in elements: + for symbol in symbols: + element=element.replace(symbol,"") + if len(element): + words.append(element) + print("正常英文单词",words) + 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_courter=[] + for courter in text: + if '\u4e00'<= courter <='\u9fff': + cn_courter.append(courter) + counter_text={} + for i in set(cn_courter): + counter_text[i]=cn_courter.count(i) + return sorted(counter_text.items(),key=lambda x: x[1],reverse=True) + +def stats_text(text): + """输出合并词频统计结果""" + return stats_text_en(text) + stats_text_cn(text) + + + +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 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! +''' +cn_text=""" +python学习 +好好学习 +天天向上 +""" +if __name__ == "__main__": + first=stats_text_en(en_text) + print("统计英文单词出现次数: ",first) + second=stats_text_cn(cn_text) + print("统计汉字出现次数: ",second) From 7712f18e3372e08b2a47ab36dc23e5c6fe61792d Mon Sep 17 00:00:00 2001 From: huangmengzhu <654823607@qq.com> Date: Tue, 19 Nov 2019 22:56:31 +0800 Subject: [PATCH 4/4] day8 day8 --- exercises/1901100209/d08/main.py | 15 ++++ .../1901100209/d08/mymodule/stats_word.py | 74 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 exercises/1901100209/d08/main.py create mode 100644 exercises/1901100209/d08/mymodule/stats_word.py diff --git a/exercises/1901100209/d08/main.py b/exercises/1901100209/d08/main.py new file mode 100644 index 000000000..8397cd117 --- /dev/null +++ b/exercises/1901100209/d08/main.py @@ -0,0 +1,15 @@ +from mymodule import stats_word +import traceback + +def test_traceback(): + try: + stats_word.stats_text(1) + except Exception as e: + print('texs_traceback =>',e) + print(traceback.format_exc()) + + + + +if __name__ == '__main__': + test_traceback() \ No newline at end of file diff --git a/exercises/1901100209/d08/mymodule/stats_word.py b/exercises/1901100209/d08/mymodule/stats_word.py new file mode 100644 index 000000000..d12ed1b0f --- /dev/null +++ b/exercises/1901100209/d08/mymodule/stats_word.py @@ -0,0 +1,74 @@ +def stats_text_en(text): + """统计参数中英文单词出现的次数,并按降序排列数组""" + if not isinstance(text,str): + raise ValueError("参数必须是字符串类型,输入类型 %s" % type(text)) + elements=text.split() + words=[] + symbols=",.*-!" + for element in elements: + for symbol in symbols: + element=element.replace(symbol,"") + if len(element) and element.isascii(): + words.append(element) + print("正常英文单词",words) + 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): + """统计汉字出现的次数,并按降序排列数组""" + if not isinstance(text,str): + raise ValueError("参数必须是字符串类型,输入类型 %s" % type(text)) + cn_courter=[] + for courter in text: + if '\u4e00'<= courter <='\u9fff': + cn_courter.append(courter) + counter_text={} + for i in set(cn_courter): + counter_text[i]=cn_courter.count(i) + return sorted(counter_text.items(),key=lambda x: x[1],reverse=True) + +def stats_text(text): + """输出合并词频统计结果""" + if not isinstance(text,str): + raise ValueError("参数必须是字符串类型,输入类型 %s" % type(text)) + return stats_text_en(text) + stats_text_cn(text) + + + +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 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! +''' +cn_text=""" +python学习 +好好学习 +天天向上 +""" +if __name__ == "__main__": + first=stats_text_en(en_text) + print("统计英文单词出现次数: ",first) + second=stats_text_cn(cn_text) + print("统计汉字出现次数: ",second)