Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions exercises/1901050090/1001S02E05_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
list_a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sort_list=list_a[::-1]
print('翻转后的数组',sort_list)

join_str=''.join([str(i) for i in sort_list])

print('数组拼接成字符串',join_str)

cut_str=join_str[2:8]

print('取第三个到第八个字符',cut_str)

rev_str=cut_str[::-1]

print('翻转后的字符串',rev_str)

int_value=int(rev_str)

print('转化为int类型',int_value)

print('转化为 二进制',bin(int_value))

print('转化为 八进制',oct(int_value))

print('转化为 十六进制',hex(int_value))

44 changes: 44 additions & 0 deletions exercises/1901050090/1001S02E05_stats_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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))

41 changes: 41 additions & 0 deletions exercises/1901050090/1001S02E05_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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!
'''
text1=text.replace('better','worse')

print('better替换为worse',text1)

words=text1.split()
filtered=[]
for word in words:
if word.find('ea')<0:
filtered.append(word)
print('将单词中的ea剔除',filtered)

counter={}

words_set=set(filtered)
swapcase=[i.swapcase() for i in filtered]

print('进行大小写翻转',swapcase)

print('单词按照a--z升序排列',sorted(swapcase))


65 changes: 65 additions & 0 deletions exercises/1901050090/1001S02E06_stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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_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='''
昨天上午,根据导航显示的“玉泉路1号”,
我来到植物园北门的一处停车场附近,
来回找了十来分钟都没找到“故居”。
植物园内的指路牌,
也没有任何蔡元培故居的标志或提示。
询问停车场工作人员,一连问了五人,无论是“玉泉路1号”还是“蔡元培故居”,
他们都摇头说不知道;又去问植物园门口小卖部里的阿姨,一位阿姨说:“故居没听说过,
你要么往植物园公交站那里去看看,好像有两幢别墅洋房。
'''

#if __name__== '_main_':
en_result=stats_text_en(en_text)
cn_result=stats_text_cn(cn_text)
print('统计参数中每个英文单词出现的次数',en_result)
print('统计参数中每个中文汉字单词出现的次数',cn_result)


67 changes: 67 additions & 0 deletions exercises/1901050090/d07/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from mymodule import stats_word

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_text=stats_word.stats_text(text)
print('统计结果',result_text)
37 changes: 37 additions & 0 deletions exercises/1901050090/d07/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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_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)


#if __name__== '_main_':
def stats_text(text):
en_result=stats_text_en(text)
cn_result=stats_text_cn(text)
#print('统计参数中每个英文单词出现的次数',en_result)
#print('统计参数中每个中文汉字单词出现的次数',cn_result)

return en_result + cn_result


10 changes: 10 additions & 0 deletions exercises/1901050090/d08/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from mymodule import stats_word
import traceback
if __name__ == "__main__":
try:
result_text=stats_word.stats_text(1)
except Exception as e:
print('test_traceback-->',e)
print(traceback.format_exc())


43 changes: 43 additions & 0 deletions exercises/1901050090/d08/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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,'')
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):
if not isinstance(text,str):
raise ValueError('参数必须是str类型,输入的类型 %s' %type(text))
cn_characters=[]
for character in text:
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)


#if __name__== '_main_':
def stats_text(text):
if not isinstance(text,str):
raise ValueError('参数必须是str类型,输入的类型 %s' %type(text))
en_result=stats_text_en(text)
cn_result=stats_text_cn(text)
#print('统计参数中每个英文单词出现的次数',en_result)
#print('统计参数中每个中文汉字单词出现的次数',cn_result)

return en_result + cn_result


29 changes: 29 additions & 0 deletions exercises/1901050090/d09/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from mymodule import stats_word
from os import path
import json
import logging

logging.basicConfig(
format='文件名:%(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_path)

with open (file_path,'r',encoding='utf-8') as f:
return f.read()

def main():
try:
data=load_file()
# print(data)
logging.info(data[0])
data1=json.loads(data)
poems=''
for item in data1:
poems += item.get('contents','')
logging.info(stats_word.stats_text_cn(poems,100))
except Exception as e:
logging.exception(e)
if __name__ == "__main__":
main()
Loading