1.如何随机生成验证码,快速生成内推码
import random
import string
code_str= string.ascii_letters +string.digits
print(code_str)
def gen_code(len=4):
return "".join (random.sampl(code_str.len)
print([gen_code(len=6) for i in range(10)])
print({gen_code(len=6) for i in range(10)})
2.高阶函数:变量可以指向函数,函数的参数能接收变量,那么一个函数就能接受另一个函数作为参数,这种函数就称为高阶函数 。
def hfunc(x,y,f):
print( f(x) ,f(y))
hfunc(2,-10,abs)
3.恺撒加密与暴力破解
import string
def kaisacrypt(text='hello', k=3):
lower =string.ascii_lowercase[k:] +string.ascii_lowercase[:k]
upper =string.ascii_uppercase[k:] +string.ascii_uppercase[:k]
d = srt. maketrans(string.ascii_letters,lower+upper)
return text.translate(d)
cryptStr=kaisacrypt()
print(cryptStr)
破解
def check (text):
mostCommonWords=('the','is','to','not','have','than','for','ok')
return len([1 for word in mostCommonWards if word in text]) >2
def bruteForce (text):
for i in range(26):
t =kaisacrypt(text, -i)
if check(t)
print(i)
print(t)
break
text="If not to the sun for smilling , warm is still in the sun there"
cryptStr=kaisacrypy(text=text,k=18)
print(cryptStr)
bruteForce(cryptStr)
4.基于缓冲区的生产消费者模型
import random
impot time
def consumer(name):
print("%s准备包子"%(name))
while True:
kind=yield
print("%s购买%s包子成功"%(name,kind))
def producer(name):
c1=consumer("用户1")
c2=consumer("用户2")
next(c1)
next(c2)
cacheList =[]
cacheListLen=5
def isfull():
return len(cacheList)==5
def consumer(name):
print("%s准备买包子" %(name))
while True:
kind =yield
print("%s购买%s包子成功" %s(name,kind))
def producer(name):
print("%s厨师正在生产包子"%(name))
kind = ["A","B","C","D"]
while True:
if not isfull():
time.sleep(random.random())
kind = random.chice(kinds)
print("%s已经生产好的%s包子" %s(name ,kind))
cacheList.append(kind)
else:
print("已经有足够的包子")
yield
p= producer("hello")
next(p)
consumeer =[consumer('user'+str(i) for i in range (10))]
for i in consumers:
if not cacheList:
print("目前没有包子")
else:
if not isfull():
next(p)
kind = cacheList.pop()
next(i)
i.send(kind)
producer("fentiao")
5.高阶函数:变量可以指向函数,函数的参数能接收变量,那么一个函数就能接受另一个函数作为参数,这种函数就称为高阶函数 。
高阶函数: 实参是一个函数名;函数的返回值是一个函数
def hfunc(x,y,f):
print( f(x) ,f(y))
hfunc(2,-10,abs)
对于序列每个元素求阶乘
import random
def factoria(x):
res =1
for i in range(1,x+1):
res=res*i
return res
li=[random.randint(2,7) for i in range(10)]
print(list(map(factoria,li)))
拿出1~100之间所有素数
filter函数
def isPrime(num):
for i in range (2,num)
if num %i==0:
return False
else:
return True
print(list(filter(isPrime,range(2,101))))
li.sort()和sorted()两种方法的区别
列表里提供了sort方法,其它数据结构没有。sorted可以对任何迭代对象排序
sort支持原地排序(变量排序后,变量本身改变)sorted排序后返回一个新列表
info= [
('apple3', 200,32),
('apple4', 1,12),
('apple1', 40,2) ,
('apple2', 100,23)
]
print(sorted(info))
def sorted_by_count(x)
return [1]
print(sorted(info,key=sorted_by_count))
def sorted_by_price(x)
return [2]
print(sorted(info,key=sorted_by_price))
def sorted_by_count_price(x)
return [1], [2]
print(sorted(info,key=sorted_by_count_price))
匿名函数 lambda 形参:返回值
from functools import reduce
print(reduce(lambda x,y: x+y, [1,2,3,4,5]))
print(sorted(info,key=lambda x: x[1]))
print(sorted(info,key=lambda x: x[2]))
print(sorted(info,key=lambda x:( x[1],x[2]))
from operator import itemgetter
print(sorted(info,key=itemgetter(1)))
print(sorted(info,key=itemgetter(2)))
print(sorted(info,key=itemgetter(1,2)))
print(sorted(info,key=itemgetter(1,-2)))
d={
'003':{
'name': 'apple1',
'count':100,
'price':10
}
'002':{
'name': 'apple1',
'count':200,
'price':2
}
}
print(d.items())
print(sorted(d.items(),key=lambda x: x[1]['count']))
print(sorted(d.items(),key=lambda x: x[1]['price']))
print(sorted(d.values(),key=lambda x: x['count']))
print(sorted(d.values(),key=lambda x: x['price']))
from operator import itemgetter
print(sorted(d.values(),key=itemgetter ('count')))
print(sorted(d.values(),key=itemgetter ('price')))
print(sorted(d.values(),key=itemgetter ('price','count')))
一个数列,把所有奇数放前面,偶数放后面
import random
li=[random.randint(1,10) for i in range(10)]
print(li)
print(sorted(li,key=lambda x: 1 if x%2==0 else 0 ))
print(sorted(li,key=lambda x: x%2==0))
l=['heello','hjded','dederfref']
print(max(l,key=lambda x: len(x)))
print(min(l,key=lambda x: len(x)))