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
1 change: 1 addition & 0 deletions exercises/PanChuang2019/1001S02E01_helloworld.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Human life is fleeting
1 change: 1 addition & 0 deletions exercises/PanChuang2019/1001S02E02_hello_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hello world!')
1 change: 1 addition & 0 deletions exercises/PanChuang2019/1001S02E02_hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello_Word")
15 changes: 15 additions & 0 deletions exercises/PanChuang2019/1001S02E03_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
operator = input('请输入运算符(+、-、*、/):')
firstdata = input('请输入第一个数字:')
seconddata = input('请输入第二个数字:')
x = int(firstdata)
y = int(seconddata)
if operator=="+":
print(x,"+",y,"=",x+y)
elif operator=="-":
print(x,"-",y,"=",x-y)
elif operator=="*":
print(x,"*",y,"=",x*y)
elif operator=="/":
print(x,"/",y,"=",x/y)
else:
print("运算符输入错误")
13 changes: 13 additions & 0 deletions exercises/PanChuang2019/1001S02E04_control_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
for i in range(1,10):
for j in range(1,i+1):
print('{}x{}={}'.format(i,j,i*j),end='\t')
print()

for i in range(1,10):
while i%2==0:
break
else:
for j in range(1,i+1):
print(i,"*",j,"=",i*j,end="\t")
print()
continue
14 changes: 14 additions & 0 deletions exercises/PanChuang2019/1001S02E05_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
reversed_list=list[::-1]
print(reversed_list)
joined_str=''.join(str(i) for i in reversed_list)
print(joined_str)
sliced_str=joined_str[2:8]
print(sliced_str)
reversed_list=sliced_str[::-1]
print(reversed_list)
int_value=int(reversed_list)
print(int_value)
print(bin(int_value))
print(oct(int_value))
print(hex(int_value))
41 changes: 41 additions & 0 deletions exercises/PanChuang2019/1001S02E05_stats_text.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!
'''
elements=text.split()
word_amount=[]
symbols="*.-,!"
for element in elements:
for symbol in symbols:
element=element.replace(symbol,'')
if len(element):
word_amount.append(element)
print(word_amount)

counter={}
word_amount_set=set(word_amount)

for word in word_amount_set:
counter[word]=word_amount.count(word)
print(counter)
print("这个是什么东东\n",counter.items())
print("排序\n",sorted(counter.items(),key=lambda x:x[1],reverse=True))
35 changes: 35 additions & 0 deletions exercises/PanChuang2019/1001S02E05_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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!
'''

text_New=text.replace("better","worse")
print('better变worse 的转换',text_New)
boom=text_New.split()
filter=[]
for word in boom:
if word.find('ea')==-1:
filter.append(word)
print(filter)
swapcased = [i.swapcase() for i in filter]
print(swapcased)
print(sorted(swapcased))
69 changes: 69 additions & 0 deletions exercises/PanChuang2019/1001S02E06_stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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='''
假如生活没有给你女朋友,
不要悲伤,
不要心急,
忧郁的日子需要的是镇静,
相信吧,
娶媳妇儿的日子将会到来
...
一切都是肾虚,
一切都将过去,
而那春梦中的,
就将变成亲切的怀恋。
'''
if __name__=='__main__':
en_turnout = stats_text_en(en_text)
cn_turnout = stats_text_cn(cn_text)
print('统计参数中每个英文出现的次数\n',en_turnout)
print('统计参数中每个汉字出现的次数\n',cn_turnout)
22 changes: 22 additions & 0 deletions exercises/PanChuang2019/D07/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from mymodule import stats_word

sample_text='''
了不起的盖茨比

我年纪还轻,阅历不深的时候,我父亲教导过我一句话,
我至今还念念不忘。

“每逢你想要批评任何人的时候,”他对我说,
“你就记住,这个世界上所有的人,并不是个个都有过你拥有的那些优越条件。”

The Great Gatsby
In my younger and more vulnerable years my father gave me some advice
that I've been turning over in my mind ever since.
"Whenever you feel like criticising any one,"he told me,
"just remember that all the people in this world haven't had the advantages
that you've had."
'''

result = stats_word.stats_text(sample_text)
print('统计结果\n',result)

75 changes: 75 additions & 0 deletions exercises/PanChuang2019/D07/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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_cn(text) + stats_text_en(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='''
假如生活没有给你女朋友,
不要悲伤,
不要心急,
忧郁的日子需要的是镇静,
相信吧,
娶媳妇儿的日子将会到来
...
一切都是肾虚,
一切都将过去,
而那春梦中的,
就将变成亲切的怀恋。
'''
if __name__=='__main__':
en_turnout = stats_text_en(en_text)
cn_turnout = stats_text_cn(cn_text)
print('统计参数中每个英文出现的次数\n',en_turnout)
print('统计参数中每个汉字出现的次数\n',cn_turnout)
print(stats_text)
30 changes: 30 additions & 0 deletions exercises/PanChuang2019/D08/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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()



Loading