目录
6-1 jmu-python-组合数据类型-1.计算坐标点欧氏距离
6-10 编写函数计算f(i) = 1/2 + 2/3 + 3/4 + ... + i/(i+1) (高教社,《Python编程基础及应用》习题8-4)
6-14 编写函数计算一个或不特定多个数的乘积(高教社,《Python编程基础及应用》习题8-6)
7-2 三天打渔、两天晒网 - 实验7 简单的循环程序 - 《Python编程基础及应用实验教程》(高等教育出版社)
6-1 jmu-python-组合数据类型-1.计算坐标点欧氏距离
def readPoint() :
t=[]
cd=input().split(',')
for i in cd:
if i=='':
t.append(0)
else:
t.append(int(i))
return tuple(t)
def distance(point):
sum=0
for i in point :
sum += i**2
return sum**0.5
6-2 jmu-python-函数-找钱
def giveChange(money):
x=money//10
y=(money-(x*10))//5
z=money-(x*10)-(y*5)
print('{} = {}*10 + {}*5 + {}*1'.format(money,x,y,z))
6-3 缩写词
def acronym(p):
s=""
p=p.title()
a=p.split()
for i in range(0,len(a)):
s=s+a[i][0]
return s
6-4 jmu-java&python-统计字符个数
def Freq(line):
arr = {}
for i in line:
arr[i] = arr.get(i, 0) + 1
arr = list(arr.items())
arr.sort(key=lambda x: x[0])
print(len(arr))
for i in range(len(arr)):
m, n = arr[i]
print("{0} = {1}" .format(m, n))
6-5 平均成绩和等级
def cal_average(g1,g2,g3,g4,g5):
g=0
g=(g1+g2+g3+g4+g5)/5
return g
def cal_grade(g):
if 90<=g<=100:
n="A"
elif 80<=g<=89:
n="B"
elif 70<=g<=79:
n="C"
elif 60<=g<=69:
n="D"
else:
n="F"
return n
6-6 整数数位和
def digitSum(v):
sum=0
while v:
sum+=v%10
v//=10
return int(sum)
6-7 求期末总评成绩
def score(quiz,usual,end,quiz_rate=0.3,usual_rate=0.15):
s1=0
s1=quiz*quiz_rate+usual*usual_rate+end*(1-usual_rate-quiz_rate)
return s1
6-8 计算阶乘
def factorial(num):
if num==1:return 1
elif num==0: return 1
n=1
for i in list(range(1,num+1)):
n*=i
return n
6-9 判断素数(函数实现)
def isPrime(n):
flag = 0
if n>1 :
for i in range(2,n):
if n%i == 0:
flag=1
if flag==0:
return n
6-10 编写函数计算f(i) = 1/2 + 2/3 + 3/4 + ... + i/(i+1) (高教社,《Python编程基础及应用》习题8-4)
def f(i):
if i==1:
return 0.5
else:
a=float(i)/float(i+1)
result=a+f(i-1)
return result
6-11 凯撒加密的破解
# 只要解密的句子中的单词有60%符合字典就判断为正确解密
def Crack(text):
# 分割字符串
text = text.split(' ')
# 判断末尾是否有标点
flag = True
# 若有标点则存放起来
if not text[-1].isalpha():
flag = False
oldword = list(text[-1])
restructuring = ""
# 标点存放字符串
symbol = ""
for i in oldword:
if i.isalpha():
restructuring = restructuring + i
else:
symbol = i
text[-1] = restructuring
# 提供的单词字典
EnglishWord = ReadEnglishWords()
# 解密后句子
for x in range(1, 26):
newList = []
for i in text:
# 凯撒解密
words = Decrypt(i, x)
# 将单词组装到解密句子
newList.append(words)
# 统计句子中与字典中相同个数
count = 0
# 在字典中循环寻找对应解密的单词,如果对应则数目加一
for k in EnglishWord:
for m in newList:
if k == m.lower():
count += 1
# 判断正确比例
numbers = count / len(newList)
# 正确比例大于60%则输出,原加密句式中有标点则补上
if numbers >= 0.6:
a = " ".join(newList)
if not flag:
a = a + symbol
return a
6-12 凯撒加密解密
def Decrypt(cryptedText, numToMove):
result=""
for i in cryptedText:
# 若是大写
if i.isupper():
result+=chr((ord(i)-ord('A')-numToMove)%26+ord('A'))
# 若是小写
elif i.islower():
result+=chr((ord(i)-ord('a')-numToMove)%26+ord('a'))
else:
result+=i
return result
6-13 函数-可变长度参数
def WordCount(*word):
s = {}
for i in word:
s[i.lower()] = s.get(i.lower(), 0)+1
return sorted(s.items(), key=lambda x:(-x[1],x[0]))
6-14 编写函数计算一个或不特定多个数的乘积(高教社,《Python编程基础及应用》习题8-6)
def caculate(*t):
r=1
for x in t:
r*=x
return r
6-15 编写函数求短语的缩写词
def acronym(p):
s=""
p=p.title()
a=p.split()
for i in range(0,len(a)):
s=s+a[i][0]
return s
6-16 修改句子
def fun(doc):
pos = 0
ans = ''
# 查找第一个为字母的位置(可能存在数字等情况)
for it in range(len(doc)):
if doc[it].isalpha():
pos = it
break
else:
ans += doc[it]
# 第一个字母进行大小写转换
if 'a' <= doc[pos] <= 'z':
upper_num = ord(doc[pos]) - 32
ans += chr(upper_num)
else:
ans += doc[pos]
for it in range(pos + 1, len(doc)):
ans += doc[it]
# 字符串末尾判断是否有句号
if doc[-1] is not '.':
ans += '.'
return ans
编程题:7-1 计算存款利息
import math
date = input().split(",")
money = eval(date[0])
year = eval(date[1])
rate = eval(date[2])
interest = money * math.pow((1 + rate*0.01), year) - money
print(f"interest={interest:.2f}")
7-2 三天打渔、两天晒网 - 实验7 简单的循环程序 - 《Python编程基础及应用实验教程》(高等教育出版社)
days= int(input())
guo = 100
wang = 100
for i in range(1,days+1):
guo *=1.001
if i%5 <=2:
wang *=1.002
else:
wang *= 0.999
print('{:.5f},{:.5f}'.format(guo,wang))