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)存小,生成速度快,歡迎嘗試?。?!
補充知識: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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python按條件刪除Excel表格數(shù)據(jù)的方法(示例詳解)
本文介紹基于Python語言,讀取Excel表格文件,基于我們給定的規(guī)則,對其中的數(shù)據(jù)加以篩選,將不在指定數(shù)據(jù)范圍內(nèi)的數(shù)據(jù)剔除,保留符合我們需要的數(shù)據(jù)的方法,感興趣的朋友跟隨小編一起看看吧2024-08-08
Python中zip()函數(shù)的解釋和可視化(實例詳解)
zip() 函數(shù)用于將可迭代的對象作為參數(shù),將對象中對應的元素打包成一個個元組,然后返回由這些元組組成的列表。這篇文章主要介紹了Python中zip()函數(shù)的解釋和可視化,需要的朋友可以參考下2020-02-02
Python循環(huán)語句之break與continue的用法
這篇文章主要介紹了Python循環(huán)語句之break與continue的用法,是Python入門學習中的基礎知識,需要的朋友可以參考下2015-10-10

