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
2 changes: 2 additions & 0 deletions exercises/1901100039/1001S02E01_helloworld.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World
~��~
1 change: 1 addition & 0 deletions exercises/1901100039/1001S02E02_hello_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print ("Hello World!")
37 changes: 37 additions & 0 deletions exercises/1901100039/1001S02E03_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 这是一个只能进行加减乘除运算的初级计算器程序
# 提示用户输入运算符,判断是否正确输入,并将数值存入对应变量
while True:
operator = input('请输入运算符(+、-、*、/): ')
if operator == '+' or operator == '-' or operator == '*' or operator == '/':
break
else:
print('请输入正确的运算符')
continue
# 提示用户输入数字,判断是否正确输入,并将数值存入对应变量
while True:
Number_One = input('请输入第一个数字:')
if Number_One.isdigit():
break
else:
print('请输入正确的数字')
continue
while True:
Number_Two = input('请输入第二个数字:')
if Number_Two.isdigit():
break
else:
print('请输入正确的数字')
continue
# 将数字变量的字符类型由'Str'转换为'Int'
a = int(Number_One)
b = int(Number_Two)

# 按运算规则进行运算并输出结果
if operator == '+':
print(a,'+',b,'=',a + b)
elif operator == '-':
print(a,'-',b,'=',a - b)
elif operator == '*':
print(a,'*',b,'=',a * b)
elif operator == '/':
print(a,'÷',b,'=',a / b)
14 changes: 14 additions & 0 deletions exercises/1901100039/1001S02E04_control_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ֵ使用for...in 循环打印九九乘法表,输出:
for i in range(1,10):
for j in range(1, i + 1):
print( i,'*',j ,'=', i * j, end='\t')
print()

# 使用while循环打印九九乘法表并用条件判断把偶数行去掉
for i in range(1,10):
if (i % 2) == 0:
continue
for j in range(1, i + 1):
print ( i,'*',j ,'=', i * j, end='\t')
print()

37 changes: 37 additions & 0 deletions exercises/1901100039/1001S02E05_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 12 16:21:21 2019

@author: mtattoo
"""

# 定义数组
list_0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list_1 = list_0[::-1] # 逆转数组
print(list_1)

# 拼接字符串
list_2 = [str(i) for i in list_1]# 使用列表推导式把列表中的单个元素全部转化为str类型
list_3 = ''.join(list_2)
print (list_3)

# 取出第三到第八个字符
list_4 = list_3[2:8] # 字符串以0开始,参数最后一位数据不计入列表,故不是2:7
print (list_4)

# 对字符串进行翻转
list_r = list_4[::-1]
print (list_r)

# 将结果转化为int类型

int_value = int(list_r)

print(int_value)

# 将结果转化为二进制,八进制和十六进制,并输出

print("转换为二进制的值:", bin(int_value))
print("转换为八进制的值:", oct(int_value))
print("转换为十六进制的值:", hex(int_value))
52 changes: 52 additions & 0 deletions exercises/1901100039/1001S02E05_stats_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 8 18:10:33 2019

@author: mtattoo
"""

# 字符串的统计

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!
'''

# 统计文本中单词出现的次数

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
print (frequency)

# 按统计次数输出
frequency_order = sorted(frequency.items(),key = lambda item:item[1],reverse = True)
print(frequency_order)
47 changes: 47 additions & 0 deletions exercises/1901100039/1001S02E05_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 字符串的处理练习

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!
'''

print (text)

# 将上文中的“better”替换为“worse”
text_worse = text.replace("better","worse")
print (text_worse)

# 将上文中的“ea”剔除
text_noea = text_worse.replace("ea","")
print (text_noea)


# 将上文中的字母进行大小写翻转
text_swap = text_noea.swapcase()
print (text_swap)

# 对上文中的单词进行排序并输出
import re #引入库
s = re.sub(r'[^\w\s]','',text_noea) #正则表达式进行剔除
text_sort = sorted(s.split())
print (text_sort)
45 changes: 45 additions & 0 deletions exercises/1901100039/1001S02E06_stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 15 15:31:31 2019

@author: mtattoo
"""
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 ) # 执行封装函数
2 changes: 2 additions & 0 deletions exercises/1901100039/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Day 1
�о�һ���е�����е��Σ�������Ƶ������һ�飬ѧϰ����Ҳֻ��������£���û�н���״̬�����ͣ����꣡
46 changes: 46 additions & 0 deletions exercises/1901100039/d07/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 17 15:06:36 2019

@author: mtattoo
"""

from mymodule import stats_word
dir()

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 Taihang 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 Taihang 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. Even 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(text)
print('中英文字符统计情况如下:', result)
46 changes: 46 additions & 0 deletions exercises/1901100039/d07/mymodule/stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 15 15:31:31 2019

@author: mtattoo
"""

# 封装统计英文单词词频的函数
def stats_text_en(text):
# 统计输入英⽂单词出现的次数,最后返回⼀个按词频降序排列列的数组
elements = text.split() # 拆分所有单词
words = []
symbols = ',.*-!'
for element in elements:
for symbol in symbols:
element = element.replace(symbol,'') # 剔除字符
# 用str 类型的 isascii 方法判断是否英文单词
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:
# 以unicode中中文字符存储的范围
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)
37 changes: 37 additions & 0 deletions exercises/1901100039/d08/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 19 11:06:36 2019

@author: mtattoo
"""

from mymodule import stats_word
import traceback
import logging

text = int(input('请输入文本: '))

logger = logging.getLogger(__name__)

def test_traceback():
try:
stats_word.stats_text(text)
except Exception as errors:
print('test_traceback :', errors)
print(traceback.format_exc())

def test_logger():
try:
stats_word.stats_text(text)
except Exception as errors:
print('test_logger :', errors)
logger.exception(errors)

if __name__ == "__main__":
stats_word.stats_text(text)
test_traceback()
test_logger()

result = stats_word.stats_text(text)
print('中英文字符统计情况如下:', result)
Loading