From 217d976b22d7bcd40d8ccadd51d67505ed993a6d Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Wed, 3 Jul 2019 12:34:15 +0800 Subject: [PATCH 01/12] 1901100050 day1 work --- exercises/1901100050/1001S02E01_helloworld.txt | 1 + exercises/1901100050/ReadMe.md | 0 2 files changed, 1 insertion(+) create mode 100644 exercises/1901100050/1001S02E01_helloworld.txt create mode 100644 exercises/1901100050/ReadMe.md diff --git a/exercises/1901100050/1001S02E01_helloworld.txt b/exercises/1901100050/1001S02E01_helloworld.txt new file mode 100644 index 000000000..ecd938ba1 --- /dev/null +++ b/exercises/1901100050/1001S02E01_helloworld.txt @@ -0,0 +1 @@ +有意思 \ No newline at end of file diff --git a/exercises/1901100050/ReadMe.md b/exercises/1901100050/ReadMe.md new file mode 100644 index 000000000..e69de29bb From 3ce5ea2e96ac5004da575b2e057e8f4879a14ff3 Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Thu, 4 Jul 2019 22:54:12 +0800 Subject: [PATCH 02/12] Create 1001S02E02_hello_world.py --- exercises/1901100050/1001S02E02_hello_world.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 exercises/1901100050/1001S02E02_hello_world.py diff --git a/exercises/1901100050/1001S02E02_hello_world.py b/exercises/1901100050/1001S02E02_hello_world.py new file mode 100644 index 000000000..21e0963b2 --- /dev/null +++ b/exercises/1901100050/1001S02E02_hello_world.py @@ -0,0 +1 @@ +print ('hello world!') From 7aaa23449fede16602df052498d6750ea5228e30 Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Wed, 10 Jul 2019 12:51:56 +0800 Subject: [PATCH 03/12] Create 1001S02E03_calculator.py --- exercises/1901100050/1001S02E03_calculator.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 exercises/1901100050/1001S02E03_calculator.py diff --git a/exercises/1901100050/1001S02E03_calculator.py b/exercises/1901100050/1001S02E03_calculator.py new file mode 100644 index 000000000..f4cda3218 --- /dev/null +++ b/exercises/1901100050/1001S02E03_calculator.py @@ -0,0 +1,26 @@ +# 计算器 + +operator = input('请输入运算符(+、-、*、/):') # input 里面的字符串的作用是在等待输入的时候进行提示 +first_number = input('请输入第一个字数') +second_number = input('请输入第二个字数') + +a = int(first_number) # int(first_number) 在这里的作用是把 str 类型的 first_number 转换成 int 类型 +b = int(second_number) + +print('operator:',operator,type(operator)) +print('first_number:', type(first_number),type(a)) +print('second_unmber:',type(second_number),type(b)) + +# print('测试加法 str 法:', first_number + second_number) +# print('测试减法 str 减法:', first_number - second_number) + +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) +else: + print('无效运算符') # raise ValueError('无效的运算符') \ No newline at end of file From e0d1a10abe41a72782e9a86b9b506a0df74d153b Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Thu, 18 Jul 2019 18:42:50 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E3=80=901901100050=E3=80=91=E8=87=AA?= =?UTF-8?q?=E5=AD=A6=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013?= =?UTF-8?q?=E7=BE=A4=20DAY4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第4天作业 --- .../1901100050/1001S02E04_control_flow.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 exercises/1901100050/1001S02E04_control_flow.py diff --git a/exercises/1901100050/1001S02E04_control_flow.py b/exercises/1901100050/1001S02E04_control_flow.py new file mode 100644 index 000000000..b2f49198e --- /dev/null +++ b/exercises/1901100050/1001S02E04_control_flow.py @@ -0,0 +1,19 @@ + +print('打印九九乘法表') +for i in range(1, 10): + # print('第%d行' % i, end='\t') + for j in range(1, i + 1): + print(i, '*', j, '=', i * j, end='\t') + # print('{} * {} = {}' .format(i, j, i * j), end='\t') + print() + +print('\n打印跳过偶数行的九九乘法表') +i = 1 +while i < 10: + if i % 2 == 0: + print() + else: + for j in range(1, i + 1): + print(i, '*', j, '=', i * j, end='\t') + # print('{} * {} = {}' .format(i, j, i * j), end='\t') + i += 1 \ No newline at end of file From b26aadd3b7831a664e82aa2863a14bc3b8feb488 Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Mon, 5 Aug 2019 18:35:07 +0800 Subject: [PATCH 05/12] =?UTF-8?q?[1901100050]=20=E8=87=AA=E5=AD=A6?= =?UTF-8?q?=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013=E7=BE=A4=20DAY5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1901100050/1001S02E01_helloworld.txt | 3 +- exercises/1901100050/1001S02E05_array.py | 26 ++++++++++ exercises/1901100050/1001S02E05_stats_text.py | 48 ++++++++++++++++++ exercises/1901100050/1001S02E05_string.py | 50 +++++++++++++++++++ exercises/1901100050/ReadMe.md | 10 ++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 exercises/1901100050/1001S02E05_array.py create mode 100644 exercises/1901100050/1001S02E05_stats_text.py create mode 100644 exercises/1901100050/1001S02E05_string.py diff --git a/exercises/1901100050/1001S02E01_helloworld.txt b/exercises/1901100050/1001S02E01_helloworld.txt index ecd938ba1..589493266 100644 --- a/exercises/1901100050/1001S02E01_helloworld.txt +++ b/exercises/1901100050/1001S02E01_helloworld.txt @@ -1 +1,2 @@ -有意思 \ No newline at end of file +有意思 +[1901100050] 自学训练营学习13群 DAY5 \ No newline at end of file diff --git a/exercises/1901100050/1001S02E05_array.py b/exercises/1901100050/1001S02E05_array.py new file mode 100644 index 000000000..b590f4a88 --- /dev/null +++ b/exercises/1901100050/1001S02E05_array.py @@ -0,0 +1,26 @@ +# 1. 将数组 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 翻转 + +sample_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +reversed_list = sample_list[::-1] +print('列表翻转 ==>', reversed_list) + +# 2. 翻转后的数组拼接成字符串 +joined_str = ''.join([str(i) for i in reversed_list]) +print('翻转后的数组拼接成字符串 ==>', joined_str) + +# 3. ⽤字符串切⽚的⽅式取出第三到第⼋个字符(包含第三和第⼋个字符) +sliced_str = joined_str[2:8] +print('用字符串切片的方式取出第三到第八个字符 ==>', sliced_str) + +# 4. 将获得的字符串进⾏翻转 +reversed_str = sliced_str[::-1] +print('字符串翻转 ==>', reversed_str) + +# 5. 将结果转换为 int 类型 + +int_value = int(reversed_str) + +print('转换为 int 类型 ==>', int_value) + +print('转换为 二进制 ==>', bin(int_value)) + diff --git a/exercises/1901100050/1001S02E05_stats_text.py b/exercises/1901100050/1001S02E05_stats_text.py new file mode 100644 index 000000000..1fbc601a5 --- /dev/null +++ b/exercises/1901100050/1001S02E05_stats_text.py @@ -0,0 +1,48 @@ +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 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 = sample_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/1901100050/1001S02E05_string.py b/exercises/1901100050/1001S02E05_string.py new file mode 100644 index 000000000..3c73820c6 --- /dev/null +++ b/exercises/1901100050/1001S02E05_string.py @@ -0,0 +1,50 @@ +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 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! +''' + +# 1.将字符串样本 text ⾥的 better 全部替换成 worse + +# 调用 str 类型 的 replace 方法进行替换 + +text = sample_text.replace('better', 'worse') +print('将字符串样本里的 better 全部替换成 worse ==>',text) + +# 2.将单词中包含 ea 的单词剔除 + +# 先将字符串根据 空白字符 分割成 list, 要调用 str 类型 +words = text.split() +# 定义一个 list 类型的变量来存放过滤完的单词 +filtered = [] +# 用 for ... in 循环遍历一遍 words 里的元素然后判断单词是否包含 ea +for word in words: + # str 类型 的 find 方法 如果不包含 参数 字符则返回 -1,如果包含则返回该字符每一次出现时的索引 + if word.find('ea') < 0: + filtered.append(word) +print('将单词中包含 ea 的单词剔除 ==>', filtered) + +# 3. 进行大小写翻转 +# 利用 列表推倒式 对 str 类型的元素进行大小写翻转 +swapcased = [i.swapcase() for i in filtered] +print('进行大小写翻转 ==>', swapcased) + +# 4. 单词按 a……z 升序排列 +print('单词按 a……z 升序排列 ==>', sorted(swapcased)) +# print('单词按 a……z 升序排列 ==>', sorted(swapcased, reverse=True)) \ No newline at end of file diff --git a/exercises/1901100050/ReadMe.md b/exercises/1901100050/ReadMe.md index e69de29bb..ab80cf813 100644 --- a/exercises/1901100050/ReadMe.md +++ b/exercises/1901100050/ReadMe.md @@ -0,0 +1,10 @@ +### day5 学习体会 + +第五天的课程学习的比较久,因为学习过程中发现很多概念自己不懂,于是看了一下英文文档,后来发现找一些网络视频来看是比较快捷的,于是找了一些视频来看。 + +通过这种方法,慢慢对一些函数,以及编程的一些概念有了进一步的了解,但是还是不能理解到所有的内容。 + +觉得不能再拖了,先完成第5天的作业再说,就像小孩子学习语言一样,刚开始遇到一个词的时候,他并不懂什么意思,直接拿来用,用多了慢慢修正在脑子里的印象就可以了。 + +**总结就是,用多了就成为自己的了。** + From 9ddd9a8c6eac46a3d634f134ab8bddb799fef60a Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Mon, 5 Aug 2019 18:35:55 +0800 Subject: [PATCH 06/12] Update 1001S02E05_array.py --- exercises/1901100050/1001S02E05_array.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/1901100050/1001S02E05_array.py b/exercises/1901100050/1001S02E05_array.py index b590f4a88..c7b1b4c1f 100644 --- a/exercises/1901100050/1001S02E05_array.py +++ b/exercises/1901100050/1001S02E05_array.py @@ -23,4 +23,6 @@ print('转换为 int 类型 ==>', int_value) print('转换为 二进制 ==>', bin(int_value)) +print('转换为 八进制 ==>', oct(int_value)) +print('转换为 二进制 ==>', hex(int_value)) From 9e34b9a5870d1ef96397b26ea624fbe568f4bc47 Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Tue, 6 Aug 2019 17:40:37 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E3=80=901901100050=E3=80=91=E8=87=AA?= =?UTF-8?q?=E5=AD=A6=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013?= =?UTF-8?q?=E7=BE=A4=20DAY6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/1901100050/1001S02E06_stats_word.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 exercises/1901100050/1001S02E06_stats_word.py diff --git a/exercises/1901100050/1001S02E06_stats_word.py b/exercises/1901100050/1001S02E06_stats_word.py new file mode 100644 index 000000000..15ac634c0 --- /dev/null +++ b/exercises/1901100050/1001S02E06_stats_word.py @@ -0,0 +1,81 @@ + +# 统计参数中每个英⽂单词出现的次数 +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) + counter = {} + word_set = set(words) + + for word in word_set: + counter[word] = words.count(word) + # 函数返回值用 return 进行返回,如果没有 return 返回值则为 None + return sorted(counter.items(), key=lambda x: x[1], reverse=True) + +# 统计参数中每个中文汉字出现的次数 +def stats_text_cn(text): + cn_characters = [] + for character in text: + # unicode 中 中文 字符的范围 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + counter = {} + cn_characters_set = set(cn_characters) + for character in cn_characters_set: + counter[character] = cn_characters.count(character) + return sorted(counter.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 之父, +做也许好过不做,但不假思索就动手还不如不做, +。。。 +''' + +# 搜索 __name__ == __main__ +#一般情况下在文件内 测试 代码的时候以下面的形式进行 +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 869b7217771481b94d369d8e6fda4a7a615db19e Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Tue, 20 Aug 2019 17:42:28 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E3=80=901901100050=E3=80=91=E8=87=AA?= =?UTF-8?q?=E5=AD=A6=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013?= =?UTF-8?q?=E7=BE=A4=20DAY7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/1901100050/d07/main.py | 53 +++++++++++ .../1901100050/d07/mymodule/stats_word.py | 89 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 exercises/1901100050/d07/main.py create mode 100644 exercises/1901100050/d07/mymodule/stats_word.py diff --git a/exercises/1901100050/d07/main.py b/exercises/1901100050/d07/main.py new file mode 100644 index 000000000..c140bd4e0 --- /dev/null +++ b/exercises/1901100050/d07/main.py @@ -0,0 +1,53 @@ +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 blocked yugong’s way making it inconvenient for him and his family to get around. + +One day yugong gathered his family together and said,”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 Taixin and Mount Wanwu? Moreover, where will all the earth and rubble go?” “Dump 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’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 an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed Yugong saying,”Aren’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 compare 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 admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away. + + +''' + +result = stats_word.stats_text(sample_text) + +print('统计结果 ==>', result) \ No newline at end of file diff --git a/exercises/1901100050/d07/mymodule/stats_word.py b/exercises/1901100050/d07/mymodule/stats_word.py new file mode 100644 index 000000000..882729736 --- /dev/null +++ b/exercises/1901100050/d07/mymodule/stats_word.py @@ -0,0 +1,89 @@ + + +# 统计参数中每个英⽂单词出现的次数 +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) + counter = {} + word_set = set(words) + + for word in word_set: + counter[word] = words.count(word) + # 函数返回值用 return 进行返回,如果没有 return 返回值则为 None + return sorted(counter.items(), key=lambda x: x[1], reverse=True) + +# 统计参数中每个中文汉字出现的次数 +def stats_text_cn(text): + cn_characters = [] + for character in text: + # unicode 中 中文 字符的范围 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + counter = {} + cn_characters_set = set(cn_characters) + for character in cn_characters_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) + + +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 之父, +做也许好过不做,但不假思索就动手还不如不做, +。。。 +''' + +# 搜索 __name__ == __main__ +#一般情况下在文件内 测试 代码的时候以下面的形式进行 +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 840738c16d76e04cb5f4703efb14a0992d722f0c Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Tue, 20 Aug 2019 18:16:58 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E3=80=901901100050=E3=80=91=E8=87=AA?= =?UTF-8?q?=E5=AD=A6=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013?= =?UTF-8?q?=E7=BE=A4=20DAY7=EF=BC=88=E6=94=B9=E6=AD=A3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/1901100050/d07/mymodule/stats_word.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/1901100050/d07/mymodule/stats_word.py b/exercises/1901100050/d07/mymodule/stats_word.py index 882729736..5374e4274 100644 --- a/exercises/1901100050/d07/mymodule/stats_word.py +++ b/exercises/1901100050/d07/mymodule/stats_word.py @@ -8,7 +8,8 @@ def stats_text_en(text): for element in elements: for symbol in symbols: element = element.replace(symbol, '') - if len(element): + # 用 str 类型 的 isascii 方法判断是否是英文单词 + if len(element) and element.isascii(): words.append(element) counter = {} word_set = set(words) From da6ba360d7f2a968ee1a9104c9b1987f1ef75c75 Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Sat, 24 Aug 2019 11:02:41 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E3=80=901901100050=E3=80=91=E8=87=AA?= =?UTF-8?q?=E5=AD=A6=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013?= =?UTF-8?q?=E7=BE=A4=20DAY8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/1901100050/d08/main.py | 27 ++++++ .../1901100050/d08/mymodule/stats_word.py | 96 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 exercises/1901100050/d08/main.py create mode 100644 exercises/1901100050/d08/mymodule/stats_word.py diff --git a/exercises/1901100050/d08/main.py b/exercises/1901100050/d08/main.py new file mode 100644 index 000000000..e26bda28b --- /dev/null +++ b/exercises/1901100050/d08/main.py @@ -0,0 +1,27 @@ +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: + # print('test_logger =>',e) + logger.exception(e) + +if __name__ == "__main__": + stats_word.stats_text(1) + test_traceback() + test_logger() + + diff --git a/exercises/1901100050/d08/mymodule/stats_word.py b/exercises/1901100050/d08/mymodule/stats_word.py new file mode 100644 index 000000000..e21b621b7 --- /dev/null +++ b/exercises/1901100050/d08/mymodule/stats_word.py @@ -0,0 +1,96 @@ + + +# 统计参数中每个英⽂单词出现的次数 +def stats_text_en(text): + 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, '') + # 用 str 类型 的 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 进行返回,如果没有 return 返回值则为 None + return sorted(counter.items(), key=lambda x: x[1], reverse=True) + +# 统计参数中每个中文汉字出现的次数 +def stats_text_cn(text): + if not isinstance(text, str): + raise ValueError('参数必须是 str 类型,输入类型 %s' % type(text)) + cn_characters = [] + for character in text: + # unicode 中 中文 字符的范围 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + counter = {} + cn_characters_set = set(cn_characters) + for character in cn_characters_set: + counter[character] = cn_characters.count(character) + return sorted(counter.items(),key=lambda x: x[1], reverse=True) + +def stats_text(text): + ''' + 合并 英文词频 和 中文词频 的结果 + ''' + if not isinstance(text, str): + raise ValueError('参数必须是 str 类型,输入类型 %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 之父, +做也许好过不做,但不假思索就动手还不如不做, +。。。 +''' + +# 搜索 __name__ == __main__ +#一般情况下在文件内 测试 代码的时候以下面的形式进行 +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 7f251a3780166f4d5a1e84d90de1e72c0e48ff0f Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Sun, 8 Sep 2019 22:58:14 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E3=80=901901100050=E3=80=91=E8=87=AA?= =?UTF-8?q?=E5=AD=A6=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013?= =?UTF-8?q?=E7=BE=A4=20DAY9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/1901100050/d08/main.py | 2 +- exercises/1901100050/d09/main.py | 35 +++++++++++++++++++ .../1901100050/d09/mymodule/stats_word.py | 34 ++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 exercises/1901100050/d09/main.py create mode 100644 exercises/1901100050/d09/mymodule/stats_word.py diff --git a/exercises/1901100050/d08/main.py b/exercises/1901100050/d08/main.py index e26bda28b..3791ffe50 100644 --- a/exercises/1901100050/d08/main.py +++ b/exercises/1901100050/d08/main.py @@ -15,7 +15,7 @@ def test_traceback(): def test_logger(): try: stats_word.stats_text(1) - except exception as e: + except Exception as e: # print('test_logger =>',e) logger.exception(e) diff --git a/exercises/1901100050/d09/main.py b/exercises/1901100050/d09/main.py new file mode 100644 index 000000000..72eaad146 --- /dev/null +++ b/exercises/1901100050/d09/main.py @@ -0,0 +1,35 @@ +from mymodule import stats_word +from os import path +import json +import re +import logging + +logging.basicConfig( + format='file:%(filename)s|line:%(lineno)d|message:%(message)s', level=logging.DEBUG) + +def load_file(): + file_path = path.join(path.dirname(path.abspath(__file__)),'tang300.json') + print('当前文件路径:', __file__, '\n读取文件路径:', file_path) + + with open(file_path, 'r', encoding='utf-8') as f: + return f.read() + +def merge_poems(date): + poems = '' + for item in date: + poems += item.get('contents', '') + return poems + +def main(): + try: + data = load_file() + logging.info(data[0]) + poems = merge_poems(json.loads(data)) + logging.info('result ==> %s', stats_word.stats_text_cn(poems,100)) + except Exception as e: + logging.exception(e) + +if __name__ == '__main__': + main() + + diff --git a/exercises/1901100050/d09/mymodule/stats_word.py b/exercises/1901100050/d09/mymodule/stats_word.py new file mode 100644 index 000000000..b9b2a02ed --- /dev/null +++ b/exercises/1901100050/d09/mymodule/stats_word.py @@ -0,0 +1,34 @@ +from collections import Counter + +# 统计参数中每个英⽂单词出现的次数 +def stats_text_en(text, count): + elements = text.split() + words = [] + symbols = ',.*-!' + for element in elements: + for symbol in symbols: + element = element.replace(symbol, '') + # 用 str 类型 的 isascii 方法判断是否是英文单词 + if len(element) and element.isascii(): + words.append(element) + return Counter(words).most_common(count) + + +# 统计参数中每个中文汉字出现的次数 +def stats_text_cn(text, count): + cn_characters = [] + for character in text: + # unicode 中 中文 字符的范围 + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + return Counter(cn_characters).most_common(count) + +def stats_text(text): + ''' + 合并 英文词频 和 中文词频 的结果 + ''' + if not isinstance(text, str): + raise ValueError('参数必须是 str 类型,输入类型 %s' % type(text)) + return stats_text_en(text, count) + stats_text_cn(text, count) + + From 6d1d9bb57c2babf669c4a7cf760d1a2a4da32727 Mon Sep 17 00:00:00 2001 From: lisq2018 Date: Sun, 22 Sep 2019 18:58:37 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E3=80=901901100050=E3=80=91=E8=87=AA?= =?UTF-8?q?=E5=AD=A6=E8=AE=AD=E7=BB=83=E8=90=A5=E5=AD=A6=E4=B9=A013?= =?UTF-8?q?=E7=BE=A4=20DAY10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/1901100050/d10/main.py | 33 +++++++++++++++++ .../1901100050/d10/mymodule/stats_word.py | 35 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 exercises/1901100050/d10/main.py create mode 100644 exercises/1901100050/d10/mymodule/stats_word.py diff --git a/exercises/1901100050/d10/main.py b/exercises/1901100050/d10/main.py new file mode 100644 index 000000000..b09e1addf --- /dev/null +++ b/exercises/1901100050/d10/main.py @@ -0,0 +1,33 @@ +from mymodule import stats_word +from os import path +import json +import re +import logging + +logging.basicConfig( + format='file:%(filename)s|line:%(lineno)d|message:%(message)s', level=logging.DEBUG) + +def load_file(): + file_path = path.join(path.dirname(path.abspath(__file__)),'tang300.json') + print('当前文件路径:', __file__, '\n读取文件路径:', file_path) + + with open(file_path, 'r', encoding='utf-8') as f: + return f.read() + +def merge_poems(date): + poems = '' + for item in date: + poems += item.get('contents', '') + return poems + +def main(): + try: + data = load_file() + logging.info(data[0]) + poems = merge_poems(json.loads(data)) + logging.info('result ==> %s', stats_word.stats_text_cn(poems,100)) + except Exception as e: + logging.exception(e) + +if __name__ == '__main__': + main() diff --git a/exercises/1901100050/d10/mymodule/stats_word.py b/exercises/1901100050/d10/mymodule/stats_word.py new file mode 100644 index 000000000..acb568440 --- /dev/null +++ b/exercises/1901100050/d10/mymodule/stats_word.py @@ -0,0 +1,35 @@ +from collections import Counter +import jieba + +# 统计参数中每个英⽂单词出现的次数 +def stats_text_en(text, count): + elements = text.split() + words = [] + symbols = ',.*-!' + for element in elements: + for symbol in symbols: + element = element.replace(symbol, '') + # 用 str 类型 的 isascii 方法判断是否是英文单词 + if len(element) and element.isascii(): + words.append(element) + return Counter(words).most_common(count) + + +# 统计参数中每个中文汉字出现的次数 +def stats_text_cn(text, count): + words = jieba.cut(text) + tmp = [] + for i in words: + if len(i) > 1: + tmp.append(i) + return Counter(tmp).most_common(count) + +def stats_text(text): + ''' + 合并 英文词频 和 中文词频 的结果 + ''' + if not isinstance(text, str): + raise ValueError('参数必须是 str 类型,输入类型 %s' % type(text)) + return stats_text_en(text, count) + stats_text_cn(text, count) + +