函数题
6-1 sdut-使用函数求a+aa+aaa++⋯+aa.....aaa(n个a)之和。
def fn(a,n):
sum = 0
for i in range(n) :
res = 0
for j in range(0, i + 1):
res = res * 10 + a
sum += res
return sum
6-2 sdut-使用函数求区域内的素数之和
def prime(p):
if p == 0 or p == 1 :
return 0
for i in range(2, int(p ** 0.5) + 1) :
if p % i == 0 :
return 0
return 1
def PrimeSum(m,n):
sum = 0
for i in range(m, n + 1) :
if prime(i) :
sum += i
return sum
6-3 sdut-使用函数统计数字字符在某数字中出现的个数
def CountDigit(number, digit):
return str(number).count(str(digit))
6-4 sdut-使用函数输出Fibonacci数列的值与指定范围内Fibonacci数值的个数
F = [1, 1]
def fib(n):
if n == 0 or n == 1:
return 1
return fib(n - 1) + fib(n - 2)
def fibs(a, b):
sum = 0
while(F[-1] <= b):
F.append(F[-1] + F[-2])
return [t for t in F if a <= t <= b]
6-5 sdut-利用函数得到缩写词
def acronym(phrase):
s = ""
ls = phrase.split()
for i in ls:
s += i[0]
s = s.upper()
return s
6-6 sdut-求嵌套列表的平均值
def Avg(lst):
ls = []
for i in lst:
ls.append(sum(i)/len(i))
return ls
编程题
7-1 sdut-求全排列
from itertools import *
n = int(input())
for s in permutations([int(x) for x in range(1, n + 1)]):
for i in str(s):
if(i.isdigit()):
print(i, end='')
print()
7-3 sdut-列表数字元素加权和(2)
def find(ls, cnt):
sum = 0
for it in ls:
if isinstance(it, list):
sum += find(it, cnt + 1)
else :
sum += it * cnt
return sum
ls = eval(input())
print(find(ls, 1))
7-4 sdut-列表或元组的数字元素求和(yeild)
def cycle(ls):
sum = 0
for it in ls:
if isinstance(it, int):
sum += it
elif isinstance(it, list):
sum += cycle(it)
elif isinstance(it, tuple):
sum += cycle(it)
return sum
ls = eval(input())
print(cycle(ls))
7-6 sdut-学生互助组队
ls_w, ls_m, ls = [], [], []
n = int(input())
for i in range(n):
id, name = input().split()
if id == '0':
ls_w.append(name)
else :
ls_m.insert(0, name)
ls.append(name)
dic1, dic2 = dict(zip(ls_w, ls_m)), dict(zip(ls_m, ls_w))
len = len(ls)//2
for i in range(len):
if ls[i] in dic1.keys():
print(ls[i], dic1[ls[i]])
else :
print(ls[i], dic2[ls[i]])
7-7 sdut-求指定层的元素个数
dic = {}
def cycle(s, n):
for it in s:
dic[n] = dic.get(n, 0) + 1
if isinstance(it, list):
cycle(it, n + 1)
s = eval(input())
n = int(input())
cycle(s, 1)
print(dic.get(n, 0))
7-6 sdut-oop-8 分数四则运算(类和对象)
from fractions import Fraction as F
n = int(input())
for i in range(n) :
a = input()
if a.find('+') != -1:
ls = a.split('+')
print(F(ls[0]) + F(ls[1]))
elif a.find('-') != -1:
ls = a.split('-')
print(F(ls[0]) - F(ls[1]))
elif a.find('*') != -1:
ls = a.split('*')
print(F(ls[0]) * F(ls[1]))
else :
ls = a.split("\\")
print(F(ls[0]) / F(ls[1]))
7-7 sdut-分数加减法
from fractions import Fraction as F
while True:
try:
a = input()
if a.find('+') != -1:
ls = a.split('+')
print(F(ls[0]) + F(ls[1]))
elif a.find('-') != -1:
ls = a.split('-')
print(F(ls[0]) - F(ls[1]))
elif a.find('*') != -1:
ls = a.split('*')
print(F(ls[0]) * F(ls[1]))
else :
ls = a.split("\\")
print(F(ls[0]) / F(ls[1]))
except :
break
文章来源地址https://www.toymoban.com/news/detail-499548.html
文章来源:https://www.toymoban.com/news/detail-499548.html
到了这里,关于Python实验五 函数、模块和包的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!