1、写出下列输出
"brouhahas"[-5 : -1] + "!"
'haha!'
“haha!”
bool("c" or "i" > "e")
True
True
#bool("c" and "i" > "e")
#True
len({
"11": "racehorse", "22":"ditto"}.items())
2
2
[n for n in range(4) if n**2 > n]
[2, 3]
[2,3]
2、写一个函数zero_sum_word(Word),它接受一个单个参数单词(一个非空字符串),如果相邻字母之间的ASCII码差之和为0,则返回True,否则汗会False,例如,"pomp"将返回True,-1(p->o), -2(o->m), 以及3(m->p), 最终(-1)+(-2)+3=0
#其实检查首尾字母相同就可以!!
#这是按照题目的方式。
def zero_sum_word(a):
if len(a) <= 1:
return True
sum = 0
for i in range(len(a)-1):
num1 = ord(a[i : i+1])
num2 = ord(a[i+1])
sum += num1 - num2
if sum == 0:
return True
else:
return False
if __name__ == '__main__':
print(zero_sum_word('pomp'))
print(zero_sum_word('supernaturals'))
print(zero_sum_word('o'))
print(zero_sum_word('disorder'))
print(zero_sum_word('supercalifragilisticexpialidocious'))
True
True
True
False
True
3、输出的第一行包含三个正整数N,M,K, 表示一个NM的矩阵乘以一个MK的矩阵。接下来N行,每行M个整数,表示第一个矩阵。再接下来的M行,每行K个整数,表示第二个矩阵。
输出格式:输出有N行,每行K个整数,表示矩阵乘法的结果。
#将每次输入的str转化为一个list,存为int
def str_to_list(a):
list1 = []
a = a.split(" ")
for i in range(len(a)):
list1.append(int(a[i]))
return list1
def matrix_multi():
#定义三个二维数组
a = []
a = str_to_list(input("请输入N,M,K:"))
#对三个数的数值限制
try:
assert a[0] > 0 and a[0] <= 100
assert a[1] > 0 and a[1] <= 100
assert a[2] > 0 and a[2] <= 100
except AssertionError as e:
print("要求0 < N,M,K <= 100,请重试!")
return
n,m,k = a[0],a[1],a[2]
matrix1 = [[0 for _ in range(m)] for _ in range(n)]
matrix2 = [[0 for _ in range(k)] for _ in range(m)]
matrix_result = [[0 for _ in range(k)] for _ in range(n)]
#传入数组值
for i in range(n):
matrix1[i] = str_to_list(input(f"请输入第一个矩阵的第{i+1}行:"))
#界限检查
try:
for x in matrix1[i]:
assert x >= 0 and x <= 1000
except AssertionError as e:
print("要求 0 <= 矩阵中的每个数 <= 1000,输入的{matrix1[i]}数值超出了限制!")
return
for j in range(m):
matrix2[j] = str_to_list(input(f"请输入第二个矩阵的第{j+1}行:"))
try:
for x in matrix2[j]:
assert x >= 0 and x <= 1000
except AssertionError as e:
print(f"要求 0 <= 矩阵中的每个数 <= 1000,输入的{matrix2[j]}数值超出了限制!")
return
#计算矩阵中每一个元素的值
for i in range(n):
for j in range(k):
sum = 0
for x in range(m):
sum += matrix1[i][x] * matrix2[x][j]
matrix_result[i][j] = sum
for i in range(n):
for j in range(k):
print(matrix_result[i][j],end = ' ')
print(end = '\n')
return
if __name__ == '__main__':
matrix_multi()
请输入N,M,K:2 1 3
请输入第一个矩阵的第1行:20
请输入第一个矩阵的第2行:2
请输入第二个矩阵的第1行:121 555 666
2420 11100 13320
242 1110 1332
class Stack:
def __init__(self):
self.stack_list = []
def push(self,item):
self.stack_list.append(item)
def pop(self):
if not self.is_empty():
a = self.stack_list[-1]
del self.stack_list[-1]
return a
else:
print("栈已经为空,无法删除元素!")
def peek(self):
if not self.is_empty():
return self.stack_list[-1]
else:
print("栈为空,栈尾无元素!")
def is_empty(self):
return len(self.stack_list) == 0
def size(self):
return len(self.stack_list)
class Quene:
def __init__(self):
self.quene_list = []
def enquene(self,item):
self.quene_list.append(item)
def dequene(self):
if not self.is_empty():
del self.quene_list[0]
else:
print("队列为空,无法删除!")
def is_empty(self):
return len(self.quene_list) == 0
def size(self):
return len(self.quene_list)
def get(self):
return self.quene_list[0]
if __name__ == '__main__':
stack1 = Stack()
stack1.push('hello')
stack1.push('python')
print(stack1.pop())
print(stack1.size())
quene1 = Quene()
quene1.enquene('你好')
quene1.enquene('中国人')
quene1.dequene()
print(quene1.get())
python
1
中国人
#一个得到最小因子的函数,递归
n = []
def get_minism_factor(a):
for i in range(a):
if i == 0 or i == 1:
continue
if a%i == 0:
n.append(i)
get_minism_factor(a//i)
return
n.append(a)
if __name__ == '__main__':
a = int(input("请输入需要分解的数字:"))
get_minism_factor(a)
print(f'{a}={n[0]}',end ='')
for i in range(len(n)-1):
print("*"+str(n[i+1]),end='')
请输入需要分解的数字:10086
10086=2*3*41*41
#题目要求只需考虑正整数情况,所以没有写小数和负数。
n = []
def transform(num,k):
if num < k:
n.append(num)
else:
n.append(num%k)
transform(num//k,k)
return
if __name__ == '__main__':
num = int(input("输入正整数num:"))
k = int(input("输入整数k:"))
transform(num,k)
for i in range(len(n)):
print(n.pop(),end='')
输入正整数num:10086
输入整数k:2
10011101100110