python课后实验(5)
课后第5次实验
1、
ls = [['Name','Age','Gender'], ['Bob','10','male'], ['Ala','12','female'], ['Cindy','12','female'], ['Dan','13','male']] f=open("out.csv","w") for l in ls: s=','.join(l)+'\n' f.write(s) f.close()
2、
f = open('out.csv','r') maxlen=0 maxS='' for line in f.readlines(): if len(line)>maxlen: line=line.replace('\n','') maxlen=len(line) maxS=line print(maxS) f.close()
3、
fo = open('data.csv','r') ls = f.readlines() ls = ls[::-1] lt = [] for item in ls: item = item.replace("\n","") item = item.replace(" ","") Is = item.split(",") lt = lt[::1] print(";".joiin(it)) f.close()
4、
f = open("阶乘.txt","w") t = 1 for i in range(1,101): t = t*i f.write(str(i)+"!="+str(t)+"\n") f.close()
5、
def lsprime(n): for i in range(2,n): if n%i==0: return False return True f=open("素数.txt","r") count = 0 s = 0 for line in f: a = line.split(",") for j in range(len(a)): a[j]=a[j].replace(" ","") if a[j]>'0':#全都在对数据进行处理 if lsprime(eval(a[j])): count = count +1 s = s+eval(a[j]) f.close() print("有{}个质数,他们的和是{}".format(count,s))
6、
def check(): if x*x+y*y==z*z or x*x+z*z==y*y or x*x==y*y+z*z: return Ture else: return False f = open("整数.txt",'r') count = 0 for line in f.readlines(): line=line.replace('\n','') a=line.split(',') if len(a)==3: x=eval(a[0]) y=eval(a[1]) z=eval(a[2]) if x>0 and y>0 and z>0: if check(x,y,z)==True: count = count + 1 print(x,y,z) f.close() print('共有{}组数据可以构成直角三角形'.format(count))
7、
f = open('整数.txt','r') count = 0 a,b,c,max=0,0,0,0 for line in f: line=line.replace('\n','') d=line.split(',') if len(d)==3: x=eval(d[0]) y=eval(d[1]) z=eval(d[2]) if x>0 and y>0 and z>0 and x+y>z and x+z>y and y+z>x: p=(x+y+z)/2 s=(p*(p-x)*(p-y)*(p-z))**0.5 if s>max: max=s a,b,c=x,y,z f.close() print('最大面积为{:.2f},边长分别{},{},{}.format(max,a,b,c)')