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: 1 addition & 1 deletion exercises/1901100053/1001S02E02_hello_python.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print("hello world"��
print("hello world"!)
21 changes: 21 additions & 0 deletions exercises/1901100053/1001S02E03_ca1cu1ator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
operator = input('请输入运算符(+、-、*、/):')
first_number = input('请输入第一位数字:')
second_number = input('请输入第二位数字:')
a = int(first_number)
b = int(second_number)
print('operator:', operator, type(operator))
print('first_number:',first_number, type(first_number),type(a))
print('second_number:',second_number,type(second_number),type(b))
print('测试加法 str 加法:', first_number + second_number)
# print('测试加法 str 减法:', first_number - second_number)
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)
else:
print('无效的运算符')
# raise valueError('无效的运算符')
19 changes: 19 additions & 0 deletions exercises/1901100053/1001S02E04_control_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

print('打印九九乘法表')
for i in range(1,10):
print('第%d行' %i,end='\t')
for j in range(1, i+1):
# print(i,'*',j,'=',i*j,end='\t')
print('{}*{}={}'.format(i,j,i*j),end='\t')
print()

# print('\n打印跳过偶数行的九九乘法表')
i = 1
while i<10:
if i % 2 == 0:
print()
else:
for j in range(1,i+1):
print(i,'*',j,'=',i*j,end='\t')
# print('{}*{}={}'.format(i,j,i*j),end='\t')
i += 1
21 changes: 21 additions & 0 deletions exercises/1901100053/day5_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 列表也叫做数组
# 1.对列表[0,1,2,3,4,5,6,7,8,9,]翻转
sample_list=[0,1,2,3,4,5,6.7,8,9,]
reversed_list=sample_list[::-1]
print('列表翻转==>', reversed_list)
# 2.翻转后的列表拼接成字符串
# 调用 '' 翻转后的列表拼接成字符串
joined_str = ''.join([str(i) for i in reversed_list])
print('翻转后的数组拼接成字符串==>', joined_str)
# 3. 用字符串切片的方式取出第三个第八个字符(包含第三和第八个字符)
sliced_str = joined_str[2:8]
print('用字符串切片的方式取出第三到第八个字符==>', sliced_str)
# 4.获得的字符串进行翻转
reversed_str = sliced_str[::-1]
print('字符串翻转==>',reversed_str)
# 5. 转换为 int 类型
int_value = int(reversed_str)
print('转换为 int 类型==>', int_value)
print('转换为 二进制==>', bin(int_value))
print('转换为 八进制==>', oct(int_value))
print('转换为 十六进制==>', hex(int_value))
64 changes: 64 additions & 0 deletions exercises/1901100053/day5_stats_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
sample_text = '''
The Zen Of Python ,by Tim peetrs


Beautiful is beter than ugly.
Explicit is better than complex.
Simple is better than complicated.
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 silenced.
Unless explcitly 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 bsd idea.
If the implementation si easy to explain,it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''

# 1. 使用字典(dict 类型)统计字符串样本 text 中各个英文单词出现的次数

# 先进将字符串根据空白字符分割成 list , 要调用 str 类型
elements = sample_text.split()

# 定义一个新的 list 型变量,存储处理过的单词
words = []

# 先针对样本挑选需要剔除的非单词符号
symbols = ',.*-!'

for element in elements:
# 遍历一遍要剔除的符号
for symbol in symbols:
# 逐个替换字符号, 用 '' 是为了同时剔除符号所占的位置
element= element.replace(symbol, '')
# 剔除了字符后 如果 element 的 长度不为 0 则算作正常单词
if len(element):
words.append(element)

print('正常的英文单词==>',words)

# 初始化一个 dict (字典)类型的变量, 用来存放单词出现的次数
counter = {}

# set (集合) 类型 可以去掉 列表里的重复元素,可以在 for...in 里减少循环次数
word_set = set(words)

for word in word_set:
counter[word]=words.count[word]

print('英文单词出现的次数==>', counter)

# 2. 按照出现次数从大到小输出所有的单词及出现的次数

# 内置函数 sorted 的参数 key 表示按元素的那一项的值进行排序
# dict 类型 counter 的 items 方法会返回一个包含相应项(key .value) 的元组 列表
# print('counter.items()==>', counter.itemse())
print('从大到小输出所有的单词及出现的次数==>', sorted(counter.items(), key = lambda x: x[1], reverse = true))
52 changes: 52 additions & 0 deletions exercises/1901100053/day5_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
sample_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 !
'''

# 1.将字符串样本里的 better 全部替换成 worse

# 调用 str 类型 的 replace 方法进行替换
text = sample_text.replace('better','worse')
print('将字符串样本里的 better 全部替换成 worse ==>',text)

# 2. 将单词中包含 ea 的单词剔除

# 先将字符串根据空白字符分割成 list ,要调用 str 类型
words = text.split()
# 定义一个 list 类型的变量用来存放过滤完的单词
filtered = []
# 用 for...in 循环遍历一遍 words 里的元素然后判断单词是否包含 ea
for word in words:
# str 类型的 find 方法 如果不包含 参数 字符则返回 -1 ,如果包含则返回该字符第一次出现时的索引
if word.find('ea') < 0:
filtered.append(word)
print('将单词中包含 ea 的单词剔除==>', filtered)

# 3. 进行大小写翻转
# 利用列表推倒式 对 str 类型的元素进行大小写翻转
swapcased = [i.swapcase() for i in filtered ]
print('进行大小写翻转 ==>', swapcased)

# 4.单词按 a_z 升序排列
# print('单词按 a_z 升序排列==>', sorted(swapcased))
print('单词按 a_z 降序排列==>', sorted(swapcased, reverse=True))
93 changes: 93 additions & 0 deletions exercises/1901100053/day6_stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 统计参数中每个英文单词出现的次数
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 进行返回, 如果没有 return 返回值则为 None
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)


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 nesed .
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 explicotly 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='''
《蟒蛇之禅》(the zen of python),蒂姆·彼得斯(tim peters)著
美丽总比丑陋好。
显式比隐式好。
简单总比复杂好。
复杂总比复杂好。
平的比嵌套的好。
稀疏总比密集好。
可读性。
特殊情况还不足以打破规则。
虽然实用性胜过纯洁性。
错误不应该悄无声息地过去。
除非明确禁止。
面对敏锐,拒绝猜测的诱惑。
应该有一种——而且最好只有一种——显而易见的方法来做到这一点。
不过,除非你是荷兰人,否则这种方式一开始可能并不明显。
现在总比不做好。
虽然从来没有比“现在”更好。
如果实现很难解释,这是一个坏主意。
如果实现易于解释,可能是个好主意。
名称空间是一个很棒的主意——让我们做更多这样的事情!
'''

# 搜索 _name_==_main_
# 一般情况下在文件内测试 代码的时候以下面的形式进行
if __name__ == '__main__':
en_result = stats_text_en(en_text)
cn_result = stats_text_cn(cn_text)
print('统计参数中每个英文单词出现的次数==>\n', en_result)
print('统计参数中每个中文汉字出现的次数==>\n', cn_result)