defsubstr():s1=input('输入子串1:')s2=input('输入子串2:')r=""m=0for i inrange(0,len(s2)):for j inrange(i+1,len(s2)+1):if s2[i:j]in s1 and m<j-i:r=s2[i:j]m=j-iprint("最长公共的子串:",r)
substr()
defisVIP(L):iflen(L)!=4:returnFalsefor i inrange(4):if L[i].isdigit()==Falseorint(L[i])<0orint(L[i])>255:returnFalsereturnTruedef_10to2(num):res=""whileTrue:res=str(num%2)+resnum=num//2if num==0:breakwhilelen(res)<8:res='0'+resreturn res
def_10to2(num):s=bin(num)res=s[2:len(s)+1].rjust(8,'0')return res
defmain():ipS=input('input IP:')L=ipS.split('.')whilenot isVIP(L):print("IP is error!")ipS=input('input IP,again:')L=ipS.split('.')s=""for i inrange(4):s=s+' '+_10to2(int(L[i]))print(s)
main()
from math import*defd(x1,y1,x2,y2):return sqrt((x1-x2)**2+(y1-y2)**2)defma():x=[1,-1,2,-2,4]y=[2,3,1.5,0,2]dd=[]s=0for i inrange(len(x)):dd.append([])for j inrange(len(x)):v=d(x[i],y[i],x[j],y[j])dd[i].append(v)if s<dd[i][j]:s=dd[i][j]for i inrange(len(x)):for j inrange(len(x)):print("%5.2f"%dd[i][j],end=" ")print()print("max=%5.2f"%s)
defma():txt=input('input text:')wordC={}for e in" !;.\t\n\"()-:#@":txt=txt.replace(e,',')L=txt.split(',')L.sort()while L[0].isdigit()or L[0]=='':del L[0]for e in L:if e in wordC:wordC[e]=wordC[e]+1else:wordC[e]=1print('按字典输出单词及次数(>2):')words=list(wordC.keys())words.sort()for e in words:if wordC[e]>2:print(e,wordC[e])print('按字出现频率排序输出(>2):')L1=list(wordC.items())L1.sort(key=gN,reverse=True)for i inrange(len(L1)):if L1[i][1]>2:print(L1[i][0],L1[i][1])defgN(x):return x[1]
验证哥德巴赫猜想:任何一个超过2偶数都可以写成两个素数之和。 问题分析: (1)将一个超过2偶数N,分解为两个数之和,如N=k1+k2,分别判断它是否为质数。 (2)找出小于N所有素数,建立一个素数表L,取出一个素数e,判断N-e in L,若为真,则找到此合数素数分解
defpm():N=int(input("请输入待验证的偶数n(n>2):"))while N<3or N%2==1:print('不符合要求!')N=int(input("请输入待验证的偶数n(n>2):"))
pme=set()for i inrange(2,N+1):pme.add(i)for i inrange(2,N+1):if i in pme:for k inrange(2*i,N+1,i):if k in pme:pme.remove(k)for e in pme:f=N-eif f>=e and f in pme:print(N,'=',e,'+',f)
利用生成器构造一个fibonacci函数,生成fibonacci的小于100的数。
deffibonacci():a=b=1yield(a)yield(b)whileTrue:a,b=b,a+byield(b)defm():for num in fibonacci():if num>100:breakprint(num, end=' ')