Skip to content
38 changes: 38 additions & 0 deletions exercises/1901100375/1001S02E06_stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
text = input('请输入英文文本:')

# 封装统计英文单词词频的函数
def stats_text_en( text ):
# 统计输入英⽂单词出现的次数,最后返回⼀个按词频降序排列列的数组
import re #引入库
text_pure = re.sub(r'[^\w\s]','',text) #正则表达式进行剔除
words = text_pure.split() # 拆分所有单词

frequency= {} # 定义频率字典
for word in words:
if word not in frequency:
frequency [word] = 1
else:
frequency [word] += 1
frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True)
print('您此次输入的文本词频降序排列如下:', frequency_order)
stats_text_en( text ) # 执行封装函数


text = input('请输入中文文本:')
# 封装统计中文汉字次数的函数
def stats_text_cn( text ):
cn_characters = []
for character in text:
# 以unicode中中文字符存储的范围
if '\u4e00' <= character <= '\u9fff':
cn_characters.append(character)
frequency= {} # 定义频率字典
for cn_characters in text:
if cn_characters not in frequency:
frequency [cn_characters] = 1
else:
frequency [cn_characters] += 1
frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True)
print('您此次输入的文本词频降序排列如下:', frequency_order)

stats_text_cn( text ) # 执行封装函数
57 changes: 57 additions & 0 deletions exercises/1901100375/d07/mymodule/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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.'''

import stats_word

stats_word.stats_word(text)
89 changes: 89 additions & 0 deletions exercises/1901100375/d07/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#1.封装统计英文单词词频的函数
text1='''
Remember the day I borrowed your brand new car and dented it?
I thought you'd kill me
But you didn't.
And remember the time I dragged you to the beach
And you said it would rain, and it did?
I thought you'd say, "I told you so."
But you didn't.

Do you remember the time I flirted with all the guys?
To make you jealous, and you were?
I thought you'd leave,
But you didn't.

Do you remember the time I spilled strawberry pie all over your car rug?
I thought you'd hit me.
But you didn't.

And remember the time I forgot to tell you the dance was formal
And you showed up in jeans?
I thought you'd drop me.
But you didn't.

Yes, there were lots of things you didn't do.
But you put up with me, and loved me, and protected me.
There were lots of things I wanted to make up to you when you returned from Vietnam.
But you didn't.
'''

def stats_text_en(text):
for x in text:
for x in ',','.','?','"','!',',','。','?','!',':','「','」':
text=text.replace(x,"")
text2=text1.split() #以空格拆分为独立单词

for i in text2:
if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文
text2.remove(i) #将列表中中文删除

dic={}
for i in text2: #将字符串转换为字典
count=text2.count(i)
r1={i:count}
dic.update(r1)
dic1=sorted(dic.items(),key=lambda x:x[1],reverse=True) #按照单词出现次数,从大到小排序
return(dic1)
print(stats_text_en(text1)) #调用函数并打印结果



#2.封装统计中文汉字字频的函数
text_1 = '''
The Zen of Python, by Tim Peters
美丽 is better than 丑陋.
清楚 is better than 含糊.
简单 is better than 复杂.
复杂 is better than 难懂.
单一 is better than 嵌套.
稀疏 is better than 稠密.
可读性计数.
Special cases aren't special enough to 打破规则.
即使练习会使得不再纯粹.
但错误不应该用沉默来掩盖.
Unless explicitly silenced.
面对起义,拒绝猜的诱惑.
有且只有一个办法.
Although that way may not be obvious at first unless you're Dutch.
尝试总比从未试过要强.
Although never is often better than *right* now.
如果执行很难被解释,那将是一个很糟的想法.
如果执行很容易解释,这会是一个好点子.
Namespaces are one honking great idea -- 让我们继续为之努力!
'''

def stats_text_cn(text): #定义检索中文函数
cndic={}
for i in text:
if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文
count = text.count(i)
cndic2 = {i:count}
cndic.update(cndic2)
cndic=sorted(cndic.items(),key=lambda item:item[1],reverse = True)
return cndic
print(stats_text_cn(text_1)) #调用函数并打印结果

def stats_word(text): #定义函数,实现统计汉字和英文单词出现次数
print(stats_text_en(text))
print(stats_text_cn(text))
stats_word(text_1)
63 changes: 63 additions & 0 deletions exercises/1901100375/d08/mymodule/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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.'''

import stats_word

try:
stats_word.stats_word(text)
print("do stats_word.")
except ValueError:
print("Stats word has error.")
finally:
print("done.")
92 changes: 92 additions & 0 deletions exercises/1901100375/d08/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#1.封装统计英文单词词频的函数
text1='''
Remember the day I borrowed your brand new car and dented it?
I thought you'd kill me
But you didn't.
And remember the time I dragged you to the beach
And you said it would rain, and it did?
I thought you'd say, "I told you so."
But you didn't.

Do you remember the time I flirted with all the guys?
To make you jealous, and you were?
I thought you'd leave,
But you didn't.

Do you remember the time I spilled strawberry pie all over your car rug?
I thought you'd hit me.
But you didn't.

And remember the time I forgot to tell you the dance was formal
And you showed up in jeans?
I thought you'd drop me.
But you didn't.

Yes, there were lots of things you didn't do.
But you put up with me, and loved me, and protected me.
There were lots of things I wanted to make up to you when you returned from Vietnam.
But you didn't.
'''

def stats_text_en(text):
for x in text:
for x in ',','.','?','"','!',',','。','?','!',':','「','」':
text=text.replace(x,"")
text2=text1.split() #以空格拆分为独立单词

for i in text2:
if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文
text2.remove(i) #将列表中中文删除

dic={}
for i in text2: #将字符串转换为字典
count=text2.count(i)
r1={i:count}
dic.update(r1)
dic1=sorted(dic.items(),key=lambda x:x[1],reverse=True) #按照单词出现次数,从大到小排序
return(dic1)
# print(stats_text_en(text_1)) #调用函数并打印结果



#2.封装统计中文汉字字频的函数
text_1 = '''
The Zen of Python, by Tim Peters
美丽 is better than 丑陋.
清楚 is better than 含糊.
简单 is better than 复杂.
复杂 is better than 难懂.
单一 is better than 嵌套.
稀疏 is better than 稠密.
可读性计数.
Special cases aren't special enough to 打破规则.
即使练习会使得不再纯粹.
但错误不应该用沉默来掩盖.
Unless explicitly silenced.
面对起义,拒绝猜的诱惑.
有且只有一个办法.
Although that way may not be obvious at first unless you're Dutch.
尝试总比从未试过要强.
Although never is often better than *right* now.
如果执行很难被解释,那将是一个很糟的想法.
如果执行很容易解释,这会是一个好点子.
Namespaces are one honking great idea -- 让我们继续为之努力!
'''

def stats_text_cn(text): #定义检索中文函数
cndic={}
for i in text:
if u'\u4e00' <= i <= u'\u9fff':#判断是不是中文
count = text.count(i)
cndic2 = {i:count}
cndic.update(cndic2)
cndic=sorted(cndic.items(),key=lambda item:item[1],reverse = True)
return cndic
#print(stats_text_cn(text_1)) #调用函数并打印结果

def stats_word(text): #定义函数,实现统计汉字和英文单词出现次数
if not isinstance(text, str):
exception = ValueError("input is not string.")
raise exception
print(stats_text_en(text))
print(stats_text_cn(text))
# stats_word(text_1)
38 changes: 38 additions & 0 deletions exercises/1901100375/d09/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
text1 = '''
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!
红酥手,黄藤酒,满城春色宫墙柳。等闲识得东风面,万紫千红总是春。
'''

from mymodule.stats_word import stats_text


stats_text()










Loading