1.
要求:在不增加或修改任何print语句,且函数内只能增加一条语句的情况下,输出:"eric"。
1)只能修改f1()函数情况下,完成上述要求
name = 'lzl'
def f1():
name = 'eric'
print(name)
def f2():
name = 'eric'
f1()
f2()
2)只能修改f2()函数情况下,完成上述要求
name = 'lzl'
def f1():
print(name)
def f2():
global name
name = 'eric'
f1()
f2()
2.验证哥德巴赫猜想:(输入任意大于2的偶数,找出两个素数,其和等于输入的偶数。若找到输出这两个素数,否则输出找不到)这个问题可分解成两个步骤:1)判断正整数是否为素数,2)任意偶数是否能分解为两个素数之和。
def isPrime(q):
if q <=1 :
return False
for i in range(2,q//2 + 1):
if q % i == 0:
return False
return True
def createPrime(n):
result = []
for i in range(2,n+1):
if isPrime(i):
result.append(i)
return result
n = 10
result = createPrime(n)
for i in result:
for j in result:
if i + j == n:
print(i,j)
3.递归、递推、lambda函数。题目:计算表达式 ,x由用户通过键盘输入,要求精确到10-7,即数列和的最后一项的绝对值小于等于10-7为止。 。
递归
i = -1
n = 1
def power(n,x):
global i
print(i)
i += 1
if 1/(n ** x) > 1e-7 and i %2 == 0:
return power(n+1,x) + 1/(n ** x)
elif 1/(n ** x) > 1e-7 and i %2 != 0:
return power(n+1, x) - 1/(n ** x)
else:
return 1/(n ** x)
x = int(input('输入x:'))
result = power(n,x)
print(result)
递推
def power(n,x):
result = 0
i = 0
while 1/(n ** x) > 1e-7:
if i %2 == 0:
i+= 1
result += 1/(n ** x)
else:
i+=1
result -= 1/(n ** x)
n += 1
return result
n = 1
x = int(input('输入x:'))
result = power(n,x)
print(result)
# Lambda
x = int(input('输入x:'))
n = 1
result = map(lambda n,y=0: y +((-1) ** (n+1)) * (1 / (n ** x)) if float(1/(n ** x)) > 1e-7 else 0, range(1,10000))
print(sum(result))
4.题目:输入字符串26个英文字母,围成一圈,以字母a为1,依顺时针方向计数数到5的倍数时,将对应的字母划去,直到所有字母都被划去时为止。思路:可用列表实现或直接用字符串切片实现
s = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
point =0
number = 1
while len(s) > 1:
if(number % 5 == 0):
print(s[point])
s.remove(s[point])
point -=1
number = 0
point +=1
number +=1
if point > len(s) - 1:
point = 0
print(s[0])
5.杨辉三角形
trangle = []
for i in range(1,9):
if i == 1:
list0 = [1]
elif i == 2:
list0 = [1,1]
else:
list0 = [1] * i
for j in range(1,i-1):
list0[j] = trangle[-1][j-1] + trangle[-1][j]
trangle.append(list0)
for i in trangle:
print(i)
6.题目:分类统计字符出现的次数(用字典统计)要求:
1)输入一串字符,统计并输出字符串中各字符出现的次数。
2)分类:字母letter,空格space,数字digit,其它字符other(用字典输出)。
# 频数统计
s = 'dgfrevgfdkgdsjgringvckghiregkjfdhgierghjkdfng'
count_dict = {}
for i in s:
if i not in count_dict.keys():
count_dict[i] = 1
else:
count_dict[i] += 1
print(count_dict.items())
# 分类统计
s = 'sdfg343dgfd 345fg 6sdf4!$ @#$!'
count_dict = {'字母': 0, '空格': 0, '数字': 0, '其他字符': 0, }
for i in s:
if i.isalpha():
count_dict['字母'] += 1
elif i.isspace():
count_dict['空格'] += 1
elif i.isdigit():
count_dict['数字'] += 1
else:
count_dict['其他字符'] += 1
print(count_dict.items())
7.凯撒密码
import random
#生成10位随机字符串
def createStr():
STR = [chr(i) for i in range(65,91)] #对应字符A-Z
sa = ''
for i in range (10):
sa+=random.choice(STR)
print(sa)
return sa
#对字符串进行加密
def enCaesar(sa):
sa_enCaesar = ''
for i in sa:
if ord(i) < 91 - k:
sa_enCaesar += chr(ord(i) + k)
else:
sa_enCaesar += chr(ord(i) + k -26)
return sa_enCaesar
#写入txt
def writeFile(sa_enCaesar):
with open('加密.txt','w',encoding='utf-8') as f:
f.write(sa_enCaesar)
#读取txt
def readFile():
with open('加密.txt','r',encoding='utf-8') as e:
sa = e.readline()
return sa
#解密
def deCaesar(sa):
sa_deCaesar = ''
for i in sa:
if ord(i) >= 65+k:
sa_deCaesar += chr(ord(i) - k)
else:
sa_deCaesar += chr(ord(i) + 26 -k)
return sa_deCaesar
sa = createStr()
k = int(input('请输入位移值:'))
sa_enCaesar = enCaesar(sa)
print('加密后:')
print(sa_enCaesar)
writeFile(sa_enCaesar)
sa = readFile()
sa_deCaesar = deCaesar(sa)
print('解密后:')
print(sa_deCaesar)