Skip to content
Open
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)


29 changes: 29 additions & 0 deletions exercises/1901050090/d010/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()
39 changes: 39 additions & 0 deletions exercises/1901050090/d010/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from collections import Counter
import jieba

def stats_text_en(text,count):
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)
return Counter(words).most_common(count)

def stats_text_cn(text,count):
if not isinstance(text,str):
raise ValueError('参数必须是str类型,输入的类型 %s' %type(text))
words=jieba.cut(text)
cn_characters=[]
for i in words:
if len(i)>1:
cn_characters.append(i)
return Counter(cn_characters).most_common(count)


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

return en_result + cn_result


Loading