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简单日志处理类分享
Feb 14 Python
python用10行代码实现对黄色图片的检测功能
Aug 10 Python
Python的Flask框架应用程序实现使用QQ账号登录的方法
Jun 07 Python
基于hashlib模块--加密(详解)
Jun 21 Python
Python如何快速上手? 快速掌握一门新语言的方法
Nov 14 Python
Python的多维空数组赋值方法
Apr 13 Python
Centos下实现安装Python3.6和Python2共存
Aug 15 Python
pygame实现雷电游戏雏形开发
Nov 20 Python
python 图像平移和旋转的实例
Jan 10 Python
详解pandas库pd.read_excel操作读取excel文件参数整理与实例
Feb 17 Python
Python:合并两个numpy矩阵的实现
Dec 02 Python
pycharm激活码2020最新分享适用pycharm2020最新版亲测可用
Nov 22 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/06/25 PHP
ThinkPHP中的三大自动简介
2014/08/22 PHP
php+mysqli使用面向对象方式查询数据库实例
2015/01/29 PHP
PHP模拟登陆163邮箱发邮件及获取通讯录列表的方法
2015/03/07 PHP
php 如何禁用eval() 函数实例详解
2016/12/01 PHP
PHP中单例模式与工厂模式详解
2017/02/17 PHP
laravel请求参数校验方法
2019/10/10 PHP
js中数组Array的一些常用方法总结
2013/08/12 Javascript
jQuery选择器中含有空格的使用示例及注意事项
2013/08/25 Javascript
详解JavaScript中localStorage使用要点
2016/01/13 Javascript
JS在Chrome浏览器中showModalDialog函数返回值为undefined的解决方法
2016/08/03 Javascript
Angular4实现动态添加删除表单输入框功能
2017/08/11 Javascript
JScript实现地址选择功能
2017/08/15 Javascript
Vue press 支持图片放大功能的实例代码
2018/11/09 Javascript
Vue源码解析之数组变异的实现
2018/12/04 Javascript
如何使用puppet替换文件中的string
2018/12/06 Javascript
基于VSCode调试网页JavaScript代码过程详解
2020/07/20 Javascript
JavaScript实现浏览器网页自动滚动并点击的示例代码
2020/12/05 Javascript
[00:44]2016完美“圣”典 风云人物:Mikasa宣传片
2016/12/07 DOTA
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
2015/04/24 Python
Python数据结构之翻转链表
2017/02/25 Python
使用Django连接Mysql数据库步骤
2019/01/15 Python
检测python爬虫时是否代理ip伪装成功的方法
2019/07/12 Python
Python统计时间内的并发数代码实例
2019/12/28 Python
python zip()函数的使用示例
2020/09/23 Python
python+appium+yaml移动端自动化测试框架实现详解
2020/11/24 Python
HTML5 placeholder属性详解
2016/06/22 HTML / CSS
HTML5中的拖放实现详解
2017/08/23 HTML / CSS
HTML5拖拽功能实现的拼图游戏
2018/07/31 HTML / CSS
详解canvas.toDataURL()报错的解决方案全都在这了
2020/03/31 HTML / CSS
澳大利亚运动鞋零售商:The Athlete’s Foot
2018/11/04 全球购物
高中数学教师求职信
2013/10/30 职场文书
求职简历推荐信范文
2013/12/02 职场文书
2014国庆节商场促销活动策划方案
2014/09/16 职场文书
师德师风心得体会(2016精选篇)
2016/01/12 职场文书
nginx之内存池的实现
2022/06/28 Servers