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网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
Apr 25 Python
Python进程通信之匿名管道实例讲解
Apr 11 Python
python实现实时监控文件的方法
Aug 26 Python
Python中使用Counter进行字典创建以及key数量统计的方法
Jul 06 Python
Python实现基于PIL和tesseract的验证码识别功能示例
Jul 11 Python
python高斯分布概率密度函数的使用详解
Jul 10 Python
Django Rest framework权限的详细用法
Jul 25 Python
python中struct模块之字节型数据的处理方法
Aug 27 Python
PyCharm 无法 import pandas 程序卡住的解决方式
Mar 09 Python
GDAL 矢量属性数据修改方式(python)
Mar 10 Python
python数据库操作mysql:pymysql、sqlalchemy常见用法详解
Mar 30 Python
如何利用python web框架做文件流下载的实现示例
Jun 02 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 URL路由类实例
2013/11/12 PHP
Thinkphp关闭缓存的方法
2015/06/26 PHP
php构造方法中析构方法在继承中的表现
2016/04/12 PHP
PHP SFTP实现上传下载功能
2017/07/26 PHP
浅谈PHP5.6 与 PHP7.0 区别
2019/10/09 PHP
thinkphp5 + ajax 使用formdata提交数据(包括文件上传) 后台返回json完整实例
2020/03/02 PHP
一些Javascript的IE和Firefox(火狐)兼容性的问题总结及常用例子
2009/05/21 Javascript
js的2种继承方式详解
2014/03/04 Javascript
纯js实现遮罩层效果原理分析
2014/05/27 Javascript
JavaScript DOM操作表格及样式
2015/04/13 Javascript
jQuery实现简易的输入框字数计数功能示例
2017/01/16 Javascript
Javascript仿京东放大镜的效果
2017/03/01 Javascript
解决Extjs下拉框不显示的问题
2017/06/21 Javascript
微信小程序 上传头像的实例详解
2017/10/27 Javascript
react-router browserHistory刷新页面404问题解决方法
2017/12/29 Javascript
基于axios封装fetch方法及调用实例
2018/02/05 Javascript
vue项目base64字符串转图片的实现代码
2018/07/13 Javascript
Nodejs文件上传、监听上传进度的代码
2020/03/27 NodeJs
Vue 封装防刷新考试倒计时组件的实现
2020/06/05 Javascript
vue3+typescript实现图片懒加载插件
2020/10/26 Javascript
解决Vue-cli3没有vue.config.js文件夹及配置vue项目域名的问题
2020/12/04 Vue.js
Python 函数返回值的示例代码
2019/03/11 Python
wxPython实现绘图小例子
2019/11/19 Python
使用ITK-SNAP进行抠图操作并保存mask的实例
2020/07/01 Python
python 无损批量压缩图片(支持保留图片信息)的示例
2020/09/22 Python
阿里巴巴国际站:Alibaba.com
2016/07/21 全球购物
Can a struct inherit from another class? (结构体能继承类吗)
2014/07/22 面试题
教师自我评价范例
2013/09/24 职场文书
机电一体化专业应届生求职信
2013/11/27 职场文书
公司活动邀请函
2014/01/24 职场文书
安全生产检查通报
2014/01/29 职场文书
爱国卫生月活动总结范文
2014/04/25 职场文书
英语三分钟演讲稿
2014/08/19 职场文书
2015年售后服务工作总结
2015/04/25 职场文书
退休职工欢送会致辞
2015/08/01 职场文书
2015年中秋放假通知范文
2015/08/18 职场文书