a =[1,2,3]
b =[10,20,30]for item inzip(a,b):print(item)
(1, 10)
(2, 20)
(3, 30)
z =zip(a,b)list(z)
[(1, 10), (2, 20), (3, 30)]
求两个字符串的最大连续字串
deffunc(str1, str2):
L1 =len(str1)
L2 =len(str2)
record =[[0for i inrange(L2+1)]for j inrange(L1+1)]#print(record)print(len(record))
maxNum =0
p =0for i inrange(L1):for j inrange(L2):if str1[i]== str2[j]:
record[i+1][j+1]= record[i][j]+1if record[i+1][j+1]> maxNum:
maxNum = record[i+1][j+1]
p = i +1return str1[p-maxNum:p],maxNum, record
defFirstUnrepeat(s):'''Python编程语言'''iflen(s)==0:return-1dict={}for char in s:dict[char]=1if char notindictelsedict[char]+1for i inrange(len(s)):ifdict[s[i]]==1:return s[i]return'False! all repeated'if __name__ =="__main__":
cases =['abcd13faeb','adasdsfa','dadadasjfds','azzzzs','zzz']#答案分别为c, f,j, -1,a, 'False! all repeated'for case in cases:print(FirstUnrepeat(case))
c
f
j
a
False! all repeated
defis_legal_IP(Strs):'''''
python编程语言
'''if'.'notin Strs:returnFalseelif Strs.count('.')!=3:returnFalseelse:
flag =True
one_list = Strs.split('.')for item in one_list:try:
one_num=int(item)if one_num >=0and one_num <=255:passelse:
flag =Falseexcept:
flag =Falsereturn flag
if __name__=='__main__':
ip_list=['','12.31.127.251','10.10.0.1000','11.1.1.1','122.23.13','aa.12.1.2','12345678','adad.ass.h','12.31.127.256''']for ip in ip_list:if is_legal_IP(ip):print('{} is a legal ip address!'.format(ip))else:print('{} is not a legal ip address!'.format(ip))
is not a legal ip address!
12.31.127.251 is a legal ip address!
10.10.0.1000 is not a legal ip address!
11.1.1.1 is a legal ip address!
122.23.13 is not a legal ip address!
aa.12.1.2 is not a legal ip address!
12345678 is not a legal ip address!
adad.ass.h is not a legal ip address!
12.31.127.256 is not a legal ip address!
a ='asa'type(a)==str
True
import sys
n, m =map(int,input().strip().split())
time_weights =[]for i inrange(n):
p =list(map(int,input().strip().split()))
time_weights.append(p)
ds =[]for i inrange(m):
d =int(input().strip())
ds.append(d)print(n,m)print(time_weights)print(ds)
result =[]for d in ds:
max_weight =0for item in time_weights:if item[0]>= d and item[1]>= max_weight:
max_weight = item[1]
result.append(max_weight)for r in result:print(r)
68
53
53
print(result)
[68, 53, 53]
n, a, b, c, f0 =list(map(int,input().strip().split()))
10 0 0 0 100
n, a, b, c, f0 =list(map(int,input().strip().split()))
f1 = a * f0 +32768
f2 = a * f1 + b * f0 +32773
f_n =[f0, f1, f2]if n <3:print(f_n[n])else:for year inrange(n-2):
money = a * f_n[year+2]+ b * f_n[year+1]+ c * f_n[year]+2*(year+3)**2-(year+3)+32767
f_n.append(money)print(f_n[-1])
import sys
n, m =map(int,input().strip().split())
time_weights =[]for i inrange(n):
p =list(map(int,input().strip().split()))
time_weights.append(p)
ds =[]for i inrange(m):
d =int(input().strip())
ds.append(d)
result =[]for d in ds:
max_weight =0for item in time_weights:if item[0]>= d and item[1]>= max_weight:
max_weight = item[1]if max_weight ==0:
result.append(-1)else:
result.append(max_weight)for r in result:print(r)
n, a, b, c, f0 =list(map(int,input().strip().split()))
f1 = a * f0 +32768
f2 = a * f1 + b * f0 +32773if n ==0:print(f0)if n ==1:print(f1)if n ==0:print(f2)
F0, F1, F2 = f0, f1, f2
for year inrange(n-2):
Next = a * F2 + b * F1 + c * F2 +2*(year+3)**2-(year+3)+32767
F0, F1, F2 = F1, F2, Next
print(F2)
3 0 0 0 100
32782
1、判断是否为素数
defisPrime(number):for num inrange(number-1,1,-1):if number % num ==0:returnFalsereturnTrue
for num in[1,2,3,4,5,10,19,44,97]:print(isPrime(num), end=' ')
True True True False True False True False True
2、输出字符串的首字母大写
defSimple(strings):
res =''
Strs = strings.split()for s in Strs:
res += s[0].upper()print(res)
Simple('end of file')
Simple('Hello world')
EOF
HW
3、给定一个数组,将其中的0元素移到数组最右边,非0元素保持原有顺序不变
deffunc(L):
lenth =len(L)for index inrange(lenth):if L[index]==0:
L.pop(index)
L.insert(0,0)return L
a =[0,1,5,10,2,0,0,3,4,0]
func(a)
[0, 0, 0, 0, 1, 5, 10, 2, 3, 4]
4、数字x满足 1000 <= x <= 9999 且 x%a=0,(x+1)%b=0,(x+2)%c=0,现给定abc的值,求x
deffunc(a,b,c):
res =[]for num inrange(1000,10000):if num % a ==0and(num+1)% b ==0and(num+2)% c ==0:
res.append(num)if res ==[]:print("No such number")else:print(res)
deffunc():for i inrange(1,20):#公鸡要少于20只for j inrange(1,33):#母鸡要少于33只
z =100- i - j #小鸡的个数if5*i +3*j + z/3==100:print("公鸡:{},母鸡:{},小鸡:{}".format(i,j,z))
deffunc():for i inrange(4,16,4):#公鸡的个数是4的倍数
j =25-7/4* i # 母鸡的个数
z =100-i -j
if5*i +3*j + z/3==100:print(i,int(j),int(z))
func()
4 18 78
8 11 81
12 4 84
12、字符串反转
deffunc(strs):
s = strs[::-1]print(s)
s ='abcd'
func(s)
dcba
s
'abcd'
13、最大公约数和最小公倍数
deffunc(m,n):
m,n =(m,n)if m < n else(n,m)print(m,n)for i inrange(m,0,-1):if n%i ==0and m%i ==0:print("最大公约数:{},最小公倍数:{}".format(i, m*n//i))#最大公约数 break
deffunc():'''一个排列组合问题,个位数只能是1,3,5,7,有四种可能;
最高位不能为0,所以有7种可能,最高位到最低位之间的可以为0,8种可能。
'''for i inrange(1,10):if i ==1:print('{}位数奇数的个数为:{}'.format(i,4))else:
nums =7*8**(i-2)*4print('{}位数奇数的个数为:{}'.format(i, nums))
deffunc(strs):
result ={}for char in strs:if char notin result.keys():
result[char]=1else:
result[char]+=1for item in result.items():print(item[0],':', item[1])