python 穷举指定长度的密码例子
本程序可根据给定的字符字典,穷举指定长度的所有字符串:
defget_pwd(str,num): if(num==1): forxinstr: yieldx else: forxinstr: foryinget_pwd(str,num-1): yieldx+y strKey="abc" forxinget_pwd(strKey,3): printx
结果:
aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc
本程序占用内存小,生成速度快,欢迎尝试!!!
补充知识:Python穷举法,二分法与牛顿-拉夫逊方法求解平方根的性能对比
穷举法,二分法与牛顿-拉夫逊方法求解平方根的优劣,从左到右依次递优。
经过测试,穷举法基本超过1分钟,还没有出数据;
二分法只要区区1秒不到就出结果了。
牛顿-拉夫逊是秒出,没有任何的停顿。
numberTarget=int(input("Pleaseenteranumber:"))
numberSqureRoot=0
while(numberSqureRoot=abs(numberTarget):
break
numberSqureRoot=numberSqureRoot+1
ifnumberSqureRoot**2!=numberTarget:
print("Yournumber%sisnotaperfectsqure,thesquarerootis%s"%(numberTarget,numberSqureRoot))
else:
ifnumberTarget<0:
numberSqureRoot=-numberSqureRoot
print("Yournumber%sisaperfectsqure,thesquarerootis%s"%(numberTarget,numberSqureRoot))
print("nowwebegintocalculatethebinarysearch...")
numberTarget=int(input("Pleaseenterthenumberforbinarysearch..."))
numberSqureRoot=0
lowValue=0.0
highValue=numberTarget*1.0
epsilon=0.01
numberSqureRoot=(highValue+lowValue)/2
whileabs(numberSqureRoot**2-numberTarget)>=epsilon:
print("lowValue:%s,highValue:%s,currentValue:%s"%(lowValue,highValue,numberSqureRoot))
ifnumberSqureRoot**2=epsilon):
numberSqureRoot=numberSqureRoot-(((numberSqureRoot**2)-k)/(2*numberSqureRoot))
print("squrerootof%sis%s"%(numberTarget,numberSqureRoot))
以上这篇python穷举指定长度的密码例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。