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
47 changes: 47 additions & 0 deletions exercises/1901100023/1001S02E03_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# 这里是单行注释

''' 嘿嘿嘿
这是 (顶着开头这行开头写,开头与line2的#对齐)
多行注释
注释的作用:给人看,方便理解。不运行
'''

"""
这也是
多行注释
"""

# 计算器确定三个输入信息,分别是运算符、运算符左边的数字和运算符右边的数字

# The equal sign (=) is used to assign a value to a variable(给变量赋值)
# string(字符串) 是'' "" 里的东西
# 把内置函数 input 接收的输入字符 赋值 给变量
operator = input('请输入运算符(+、-、*、/):') # input里面字符串的作用是在等待输入的时候进行提示
first_number = input('请输入第一个数字:')
second_number = input('请输入第二个数字:')

a = int(first_number) # int(first_number)在这里的作用:把str类型 的first_number 转换成 int 类型(a type of numbers)
b = int(second_number)

# The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters
# print() 打印后 括号内,逗号分隔的部分之间无自动空格
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) # 与int类型的加法比较
# print('测试加法 str 减法:',first_number - second_number) # 错误。字符串不支持减法运算


if operator == '+': # == 值相等
print(a, '+', b, '=',a + b) #缩进:默认4空格。按tab/空格键4次
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/1901100023/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): #遍历一个从1~9的列表,不包含10。i可以理解为行
print('第%d行' % i, end='\t') # control+斜杠 可以注释掉
for j in range(1, i + 1): # j可以理解为列
print(i, '*', j, '=', i * j, end='\t')
# print('{}*{}={}'.format(i,j,i*j), end='\t')
# end='\t'表示以tad键结尾(空格符号-占8个字符位)
print()

print('\n打印跳过偶数行的九九乘法表')
i = 1 # ‘=’是赋值的意思,把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')
i += 1 # 是i=i+1 的缩写,相当于把上一个的i再加1,重新赋值给了新的i
28 changes: 28 additions & 0 deletions exercises/1901100023/1001S02E05_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
array=[0,1,2,3,4,5,6,7,8,9]
array.reverse()

array1 = [str(i) for i in array]
print(array1)
array2 = ''.join(array1)
print(array2)

array3 = array2[2:8]

array4= []
for i in array3:
array4.append(str(i))
array4.reverse()
array5=''.join(array4)
array6=[]
for i in array5:
array6.append(int(i))
print(array6)

array7=[]
array8=[]
array9=[]
for i in array6:
array7.append(bin(i))
array8.append(oct(i))
array9.append(hex(i))
print(array7, array8, array9)
37 changes: 37 additions & 0 deletions exercises/1901100023/1001S02E05_stats_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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. 9 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(',',' ').replace('.',' ').replace("'",' ').replace('--',' ').replace('*',' ')
list1 = text1.lower().split()
dict1 = {} #新建字典
for i in list1:
count = list1.count(i)
n = {i:count}
dict1.update(n)
print(dict1)
dict1 = sorted(dict1.items(), key=lambda x:x[1], reverse=True)
# x is the list, in which we are adding x[1] i.e 2nd element of list to the sort function.
# sort(mylist, key=lambda x: x[1]) sorts mylist based on the value of key as applied to each element of the list.
print(dict1)
44 changes: 44 additions & 0 deletions exercises/1901100023/1001S02E05_string.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. 9 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(text1)

text2 = text1.split(' ')
text3=[]
for i in text2:
if 'ea' not in i: #相反的思路~ 不是想着删掉啥,而是想想留下来的啥
text3.append(i)
# print(text3)

text4 = (' '.join(text3))
print(text4)

text5 = text4.swapcase()
print(text5)

text6 = text5.split()
text6.sort(key=str.lower)
print(text6)
78 changes: 78 additions & 0 deletions exercises/1901100023/1001S02E06_stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 1. 定义⼀一个名为 stats_text_en 的函数,函数接受⼀个 字符串 text 作为参数
def stats_text_en(text):

# 2. 实现该函数的功能(同day5任务2):统计参数中每个英⽂文单词出现的次数,最后返回⼀个按词频 降序 排列列的 数组
elements = text.split() # list。构成函数体的语句从下一行开始,并且必须缩进。
words = []
symbols = ',*!*.-'
for element in elements:
for symbol in symbols:
element = element.replace(symbol,'')
if len(element): # 看单词长度是否大于0,大于0则为真正的单词
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)


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


# ------------------------------------------------------------------------------------------------------------------
# 3. 定义⼀一个名为 stats_text_cn 的函数,函数接受一个字符串串 text 作为参数
# 4. 实现该函数的功能:统计参数中每个中⽂文汉字出现的次数,最后返回⼀个按字频 降序 排列的 数组
def stats_text_cn(text):
cn_characters = []
for character in text:
if '\u4e00' <= character <= '\u9fff': # unicode中文字符的范围
# 计算机中所有的字符都是有数字来表示的。汉字也是有数字表示的,
# Unicdoe4E00~9FFF表示中文
# if u'a' <= ch <= u'z' or u'A' <= ch <= u'Z':提取英文
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)


cn_text = '''
上善若水。
水善利万物而不争,处众人之所恶,故几于道。
居,善地;心,善渊;与,善仁;言,善信;政,善治;事,善能;动,善时。
夫唯不争,故无尤。
'''


# 搜索 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)