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 图片验证码代码
Dec 07 Python
高性能web服务器框架Tornado简单实现restful接口及开发实例
Jul 16 Python
Python中的集合介绍
Jan 28 Python
python的sorted用法详解
Jun 25 Python
python接口调用已训练好的caffe模型测试分类方法
Aug 26 Python
python创建学生成绩管理系统
Nov 22 Python
Python使用pymysql模块操作mysql增删改查实例分析
Dec 19 Python
Python数据可视化处理库PyEcharts柱状图,饼图,线性图,词云图常用实例详解
Feb 10 Python
tensorflow实现二维平面模拟三维数据教程
Feb 11 Python
快速解决Django关闭Debug模式无法加载media图片与static静态文件
Apr 07 Python
Python2手动安装更新pip过程实例解析
Jul 16 Python
python开发一个解析protobuf文件的简单编译器
Nov 17 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
mysq GBKl乱码
2006/11/28 PHP
php中去除所有js,html,css代码
2010/10/12 PHP
PHP显示今天、今月、上月、今年的起点/终点时间戳的代码
2011/05/25 PHP
解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别
2013/06/24 PHP
php对数组排序的简单实例
2013/12/25 PHP
PHP实现获取第一个中文首字母并进行排序的方法
2017/05/09 PHP
PHP正则删除HTML代码中宽高样式的方法
2017/06/12 PHP
javascript面向对象的方式实现的弹出层效果代码
2010/01/28 Javascript
用jquery中插件dialog实现弹框效果实例代码
2013/11/15 Javascript
浅谈checkbox的一些操作(实战经验)
2013/11/20 Javascript
JS将光标聚焦在文本最后的实现代码
2014/03/28 Javascript
js使用心得分享
2015/01/13 Javascript
javascript图片切换综合实例(循环切换、顺序切换)
2016/01/13 Javascript
利用Js+Css实现折纸动态导航效果实例源码
2017/01/25 Javascript
javascript中的面向对象
2017/03/30 Javascript
JavaScript编写的网页小游戏,很给力
2017/08/18 Javascript
详解一个基于套接字实现长连接的express
2019/03/28 Javascript
JS中的算法与数据结构之常见排序(Sort)算法详解
2019/08/16 Javascript
微信小程序 动态修改页面数据及参数传递过程详解
2019/09/27 Javascript
JS+CSS+HTML实现“代码雨”类似黑客帝国文字下落效果
2020/03/17 Javascript
js验证密码强度解析
2020/03/18 Javascript
python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix实现
2018/06/11 Python
pymysql 插入数据 转义处理方式
2020/03/02 Python
PySide2出现“ImportError: DLL load failed: 找不到指定的模块”的问题及解决方法
2020/06/10 Python
如何查看python关键字
2021/01/17 Python
使用CSS3实现SVG路径描边动画效果入门教程
2019/10/21 HTML / CSS
MATCHESFASHION.COM法国官网:英国奢侈品零售商
2018/01/04 全球购物
岗位职责的含义
2013/11/17 职场文书
大学团支书的自我评价分享
2013/12/14 职场文书
学习型党组织心得体会
2014/09/12 职场文书
党员违纪检讨书怎么写
2014/11/01 职场文书
教师创先争优承诺书
2015/04/27 职场文书
2015年远程教育工作总结
2015/05/20 职场文书
Nginx域名转发https访问的实现
2021/03/31 Servers
解决golang在import自己的包报错的问题
2021/04/29 Golang
React 高阶组件HOC用法归纳
2021/06/13 Javascript