python 窮舉指定長(zhǎng)度的密碼例子
本程序可根據(jù)給定的字符字典,窮舉指定長(zhǎng)度的所有字符串:
def get_pwd(str, num):
if(num == 1):
for x in str:
yield x
else:
for x in str:
for y in get_pwd(str, num-1):
yield x+y
strKey="abc"
for x in get_pwd(strKey,3):
print x
結(jié)果:
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
本程序占用內(nèi)存小,生成速度快,歡迎嘗試?。?!
補(bǔ)充知識(shí):Python 窮舉法, 二分法 與牛頓-拉夫遜方法求解平方根的性能對(duì)比
窮舉法, 二分法 與牛頓-拉夫遜方法求解平方根的優(yōu)劣,從左到右依次遞優(yōu)。
經(jīng)過(guò)測(cè)試,窮舉法基本超過(guò) 1 分鐘,還沒(méi)有出數(shù)據(jù);
二分法只要區(qū)區(qū)1秒不到就出結(jié)果了。
牛頓-拉夫遜是秒出,沒(méi)有任何的停頓。
numberTarget =int(input("Please enter a number:"))
numberSqureRoot = 0
while(numberSqureRoot<abs(numberTarget)):
if numberSqureRoot**2 >= abs(numberTarget):
break
numberSqureRoot = numberSqureRoot + 1
if numberSqureRoot**2 != numberTarget:
print("Your number %s is not a perfect squre, the square root is %s " % ( numberTarget,numberSqureRoot) )
else:
if numberTarget < 0 :
numberSqureRoot = -numberSqureRoot
print("Your number %s is a perfect squre, the square root is %s " % ( numberTarget, numberSqureRoot))
print("now we begin to calculate the binary search...")
numberTarget=int(input("Please enter the number for binary search..."))
numberSqureRoot = 0
lowValue = 0.0
highValue=numberTarget*1.0
epsilon = 0.01
numberSqureRoot = (highValue + lowValue)/2
while abs(numberSqureRoot**2 - numberTarget) >=epsilon:
print("lowValue:%s, highValue:%s, currentValue:%s"%(lowValue,highValue,numberSqureRoot))
if numberSqureRoot**2<numberTarget:
lowValue=numberSqureRoot
else:
highValue=numberSqureRoot
numberSqureRoot = (lowValue+highValue) /2
print("The number %s has the squre root as %s " %(numberTarget,numberSqureRoot))
print("now we begin to calculate the newTon search...")
numberTarget=int(input("Please enter the number for newTon search..."))
numberSqureRoot = 0
epsilon = 0.01
k=numberTarget
numberSqureRoot = k/2.0
while( abs(numberSqureRoot*numberSqureRoot - k)>=epsilon):
numberSqureRoot=numberSqureRoot-(((numberSqureRoot**2) - k)/(2*numberSqureRoot))
print("squre root of %s is %s " %(numberTarget,numberSqureRoot))
以上這篇python 窮舉指定長(zhǎng)度的密碼例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python函數(shù)中的可變長(zhǎng)參數(shù)詳解
- Python函數(shù)中*args和**kwargs來(lái)傳遞變長(zhǎng)參數(shù)的用法
- Python中函數(shù)的參數(shù)傳遞與可變長(zhǎng)參數(shù)介紹
- Python函數(shù)中的不定長(zhǎng)參數(shù)相關(guān)知識(shí)總結(jié)
- python print 格式化輸出,動(dòng)態(tài)指定長(zhǎng)度的實(shí)現(xiàn)
- python函數(shù)不定長(zhǎng)參數(shù)使用方法解析
- python 初始化一個(gè)定長(zhǎng)的數(shù)組實(shí)例
- Python技巧之變長(zhǎng)和定長(zhǎng)序列拆分
相關(guān)文章
Python實(shí)現(xiàn)PS濾鏡特效之扇形變換效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS濾鏡特效之扇形變換效果,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)PS濾鏡扇形變換效果的原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Python實(shí)現(xiàn)的金山快盤(pán)的簽到程序
正在學(xué)習(xí)python而且自己一直在用金山快盤(pán),所以就寫(xiě)來(lái)個(gè)簽到的功能,每天定時(shí)跑2013-01-01
Python按條件刪除Excel表格數(shù)據(jù)的方法(示例詳解)
本文介紹基于Python語(yǔ)言,讀取Excel表格文件,基于我們給定的規(guī)則,對(duì)其中的數(shù)據(jù)加以篩選,將不在指定數(shù)據(jù)范圍內(nèi)的數(shù)據(jù)剔除,保留符合我們需要的數(shù)據(jù)的方法,感興趣的朋友跟隨小編一起看看吧2024-08-08
Python 70行代碼實(shí)現(xiàn)簡(jiǎn)單算式計(jì)算器解析
這篇文章主要介紹了Python 70行代碼實(shí)現(xiàn)簡(jiǎn)單算式計(jì)算器解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python中zip()函數(shù)的解釋和可視化(實(shí)例詳解)
zip() 函數(shù)用于將可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)個(gè)元組,然后返回由這些元組組成的列表。這篇文章主要介紹了Python中zip()函數(shù)的解釋和可視化,需要的朋友可以參考下2020-02-02
Python循環(huán)語(yǔ)句之break與continue的用法
這篇文章主要介紹了Python循環(huán)語(yǔ)句之break與continue的用法,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10

