python 窮舉指定長度的密碼例子
本程序可根據(jù)給定的字符字典,窮舉指定長度的所有字符串:
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 窮舉法, 二分法 與牛頓-拉夫遜方法求解平方根的性能對比
窮舉法, 二分法 與牛頓-拉夫遜方法求解平方根的優(yōu)劣,從左到右依次遞優(yōu)。
經(jīng)過測試,窮舉法基本超過 1 分鐘,還沒有出數(shù)據(jù);
二分法只要區(qū)區(qū)1秒不到就出結(jié)果了。
牛頓-拉夫遜是秒出,沒有任何的停頓。
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 窮舉指定長度的密碼例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python自動(dòng)12306搶票軟件實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了python自動(dòng)12306搶票軟件的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02python PyQt5 爬蟲實(shí)現(xiàn)代碼
這篇文章主要介紹了python PyQt5 爬蟲實(shí)現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04python數(shù)據(jù)結(jié)構(gòu)之搜索講解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之搜索講解,搜索是指從元素集合中找到某個(gè)特定元素的算法過程。搜索過程通常返回?True?或?False,?分別表示元素是否存在,下面一起來了解文章的詳細(xì)內(nèi)容吧,希望對你有所幫助2021-12-12Python使用MoviePy實(shí)現(xiàn)編輯音視頻并添加字幕
MoviePy是一個(gè)用于視頻編輯的Python模塊,它可被用于一些基本操作,本文主要介紹了如何使用編輯音視頻并添加字幕,感興趣的小伙伴可以了解下2024-01-01人臉檢測實(shí)戰(zhàn)終極之OpenCV+Python實(shí)現(xiàn)人臉對齊
這篇文章主要是為了演示如何使用 OpenCV、Python 和面部標(biāo)志從而實(shí)現(xiàn)對齊人臉。文中示例代碼對我們的工作或?qū)W習(xí)有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12Python with關(guān)鍵字,上下文管理器,@contextmanager文件操作示例
這篇文章主要介紹了Python with關(guān)鍵字,上下文管理器,@contextmanager文件操作,結(jié)合實(shí)例形式分析了Python使用with關(guān)鍵字及上下文管理器、contextmanager進(jìn)行文件打開、讀寫、關(guān)閉等操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-10-10