python 穷举指定长度的密码例子


Posted in Python onApril 02, 2020

本程序可根据给定的字符字典,穷举指定长度的所有字符串:

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

结果:

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("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 穷举指定长度的密码例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
理解python多线程(python多线程简明教程)
Jun 09 Python
跟老齐学Python之正规地说一句话
Sep 28 Python
朴素贝叶斯算法的python实现方法
Nov 18 Python
python中子类继承父类的__init__方法实例
Dec 15 Python
快速实现基于Python的微信聊天机器人示例代码
Mar 03 Python
Python 比较两个数组的元素的异同方法
Aug 17 Python
python爬取cnvd漏洞库信息的实例
Feb 14 Python
python 进程 进程池 进程间通信实现解析
Aug 23 Python
Python序列化与反序列化pickle用法实例
Nov 11 Python
使用NumPy读取MNIST数据的实现代码示例
Nov 20 Python
python热力图实现简单方法
Jan 29 Python
PySwarms(Python粒子群优化工具包)的使用:GlobalBestPSO例子解析
Apr 05 Python
python3安装OCR识别库tesserocr过程图解
Apr 02 #Python
python简单的三元一次方程求解实例
Apr 02 #Python
Python 线性回归分析以及评价指标详解
Apr 02 #Python
Django REST framwork的权限验证实例
Apr 02 #Python
详解Ubuntu环境下部署Django+uwsgi+nginx总结
Apr 02 #Python
在 Pycharm 安装使用black的方法详解
Apr 02 #Python
Python Numpy中数据的常用保存与读取方法
Apr 01 #Python
You might like
php生成扇形比例图实例
2013/11/06 PHP
php实现查看邮件是否已被阅读的方法
2013/12/03 PHP
详解php中curl返回false的解决办法
2019/03/18 PHP
基于javascript的JSON格式页面展示美化方法
2014/07/02 Javascript
JS实现选择TextArea内文本的方法
2015/08/03 Javascript
JS基于clipBoard.js插件实现剪切、复制、粘贴
2016/05/03 Javascript
JavaScript中的ajax功能的概念和示例详解
2016/10/17 Javascript
jQuery命名空间与闭包用法示例
2017/01/12 Javascript
JavaScript requestAnimationFrame动画详解
2017/09/14 Javascript
Angular客户端请求Rest服务跨域问题的解决方法
2017/09/19 Javascript
js断点调试心得分享(必看篇)
2017/12/08 Javascript
基于vue中css预加载使用sass的配置方式详解
2018/03/13 Javascript
JavaScript对象的浅拷贝与深拷贝实例分析
2018/07/25 Javascript
AngularJS $http post 传递参数数据的方法
2018/10/09 Javascript
Vue函数式组件-你值得拥有
2019/05/09 Javascript
详解Vue-Router源码分析路由实现原理
2019/05/15 Javascript
js实现随机抽奖
2020/03/19 Javascript
JavaScript类的继承多种实现方法
2020/05/30 Javascript
关于Js中new操作符的作用详解
2021/02/21 Javascript
Python新手在作用域方面经常容易碰到的问题
2015/04/03 Python
基于python3 类的属性、方法、封装、继承实例讲解
2017/09/19 Python
解决python ogr shp字段写入中文乱码的问题
2018/12/31 Python
在Pycharm中将pyinstaller加入External Tools的方法
2019/01/16 Python
Python 脚本拉取 Docker 镜像问题
2019/11/10 Python
python3 xpath和requests应用详解
2020/03/06 Python
phpquery中文手册
2021/03/18 PHP
马来西亚在线购物:POPLOOK.com
2019/12/09 全球购物
毕业生机械建模求职信
2013/10/14 职场文书
客服文员岗位职责
2013/11/29 职场文书
法学毕业生自我鉴定
2014/01/31 职场文书
青年文明号复核材料
2014/02/11 职场文书
批评与自我批评材料
2014/02/15 职场文书
优秀教师演讲稿
2014/05/06 职场文书
党员群众路线教育实践活动剖析材料
2014/10/10 职场文书
销售助理岗位职责
2015/02/11 职场文书
食堂采购员岗位职责
2015/04/03 职场文书