From 32e0a164b13eba4c117eebb64d76ab07c55ad36a Mon Sep 17 00:00:00 2001 From: Arya21121 <15701641772@163.COM> Date: Sun, 6 Oct 2019 20:50:00 +0800 Subject: [PATCH 1/6] Create 1001S02E05_string.py --- .../1001S02E05_string.py/1001S02E05_string.py | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 exercises/1901100083/1001S02E05_string.py/1001S02E05_string.py diff --git a/exercises/1901100083/1001S02E05_string.py/1001S02E05_string.py b/exercises/1901100083/1001S02E05_string.py/1001S02E05_string.py new file mode 100644 index 000000000..eedd4dd87 --- /dev/null +++ b/exercises/1901100083/1001S02E05_string.py/1001S02E05_string.py @@ -0,0 +1,98 @@ +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. 将字符串样本里的 better 全部替换成 worse + +text = Sample_text.replace('better', 'worse') +print('将字符串样本里的 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('单词按 a_z 升序排列 ==>', sorted(swapcased)) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 96e77506dcd744bb9a4a6c28a46c30ba93443606 Mon Sep 17 00:00:00 2001 From: Arya21121 <15701641772@163.COM> Date: Sun, 6 Oct 2019 20:50:11 +0800 Subject: [PATCH 2/6] Create 1001S02E05_stats_text.py --- .../1001S02E05_stats_text.py | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 exercises/1901100083/1001S02E05_stats_text.py/1001S02E05_stats_text.py diff --git a/exercises/1901100083/1001S02E05_stats_text.py/1001S02E05_stats_text.py b/exercises/1901100083/1001S02E05_stats_text.py/1001S02E05_stats_text.py new file mode 100644 index 000000000..fe1ecf9d5 --- /dev/null +++ b/exercises/1901100083/1001S02E05_stats_text.py/1001S02E05_stats_text.py @@ -0,0 +1,108 @@ +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 中各个英文单词出现的次数 + +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) + +# 2. 按照出现次数从大到小输出所有单词及出现的次数 + +print('从大到小输出所有的单词及出现的次数 ==>', sorted(counter.items(), key=lambda x: x[1], reverse=True)) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 20c30d0e653cfda025380d91c6d57a15595890ac Mon Sep 17 00:00:00 2001 From: Arya21121 <15701641772@163.COM> Date: Sun, 6 Oct 2019 20:50:18 +0800 Subject: [PATCH 3/6] Create 1001S02E05_array.py --- .../1001S02E05_array.py/1001S02E05_array.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 exercises/1901100083/1001S02E05_array.py/1001S02E05_array.py diff --git a/exercises/1901100083/1001S02E05_array.py/1001S02E05_array.py b/exercises/1901100083/1001S02E05_array.py/1001S02E05_array.py new file mode 100644 index 000000000..cd2928694 --- /dev/null +++ b/exercises/1901100083/1001S02E05_array.py/1001S02E05_array.py @@ -0,0 +1,26 @@ +Sample_list = [0,1,2,3,4,5,6,7,8,9] + +# 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)) +print('转换为 八进制 ==>', oct(int_value)) +print('转换为 十六进制 ==>', hex(int_value)) \ No newline at end of file From 6838481efa5d21235f693b87d9cd8eabe4dcd46f Mon Sep 17 00:00:00 2001 From: Arya21121 <15701641772@163.COM> Date: Sat, 23 Nov 2019 20:37:27 +0800 Subject: [PATCH 4/6] Create 1001S02E06_stats_word.py --- .../1001S02E06_stats_word.py | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 exercises/1901100083/1001S02E06_stats_word.py/1001S02E06_stats_word.py diff --git a/exercises/1901100083/1001S02E06_stats_word.py/1001S02E06_stats_word.py b/exercises/1901100083/1001S02E06_stats_word.py/1001S02E06_stats_word.py new file mode 100644 index 000000000..8a5269a8c --- /dev/null +++ b/exercises/1901100083/1001S02E06_stats_word.py/1001S02E06_stats_word.py @@ -0,0 +1,83 @@ +# 统计参数中每个英文单词出现的次数 +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 sorted(counter.items(), key=lambda x: x[1], reverse=True) + + +# 统计参数中每个中文汉字出现的次数 +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) + + +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 之禅 by Tim Peters + +优美胜于丑陋 +明了胜于晦涩 +简洁胜于复杂 +复杂胜于凌乱 +扁平胜于嵌套 +间隔胜于紧凑 +可读性很重要 +即便假借特里的实用性之名,也不可违背这些规则 +不要包容所有错误,除非你确定需要这样做 +当存在多种可能,不要尝试去猜测 +而是尽量找一种,最好是唯一一种明显的解决方案 +虽然这并不容易,因为你不是 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 663e4c176e11db244a5547d6d576281a6a4ca084 Mon Sep 17 00:00:00 2001 From: Arya21121 <15701641772@163.COM> Date: Sun, 24 Nov 2019 21:40:24 +0800 Subject: [PATCH 5/6] Create stats_word.py --- .../d07 work/mymodule/stats_word.py | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 exercises/1901100083/d07 work/mymodule/stats_word.py diff --git a/exercises/1901100083/d07 work/mymodule/stats_word.py b/exercises/1901100083/d07 work/mymodule/stats_word.py new file mode 100644 index 000000000..2be1236aa --- /dev/null +++ b/exercises/1901100083/d07 work/mymodule/stats_word.py @@ -0,0 +1,93 @@ +# 统计参数中每个英文单词出现的次数 +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) 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: + 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) + + + + +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 之禅 by Tim Peters + +优美胜于丑陋 +明了胜于晦涩 +简洁胜于复杂 +复杂胜于凌乱 +扁平胜于嵌套 +间隔胜于紧凑 +可读性很重要 +即便假借特里的实用性之名,也不可违背这些规则 +不要包容所有错误,除非你确定需要这样做 +当存在多种可能,不要尝试去猜测 +而是尽量找一种,最好是唯一一种明显的解决方案 +虽然这并不容易,因为你不是 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 2175dd2377bf23b2c53211dee50823f2447a5c92 Mon Sep 17 00:00:00 2001 From: Arya21121 <15701641772@163.COM> Date: Sun, 24 Nov 2019 21:40:30 +0800 Subject: [PATCH 6/6] Create main.py --- exercises/1901100083/d07 work/main.py | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 exercises/1901100083/d07 work/main.py diff --git a/exercises/1901100083/d07 work/main.py b/exercises/1901100083/d07 work/main.py new file mode 100644 index 000000000..16ae45aa9 --- /dev/null +++ b/exercises/1901100083/d07 work/main.py @@ -0,0 +1,45 @@ +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