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 20 Python
Python中Threading用法详解
Dec 27 Python
python实现用户答题功能
Jan 17 Python
Python实现邮件的批量发送的示例代码
Jan 23 Python
django自带的server 让外网主机访问方法
May 14 Python
python hash每次调用结果不同的原因
Nov 21 Python
Python读入mnist二进制图像文件并显示实例
Apr 24 Python
新手学python应该下哪个版本
Jun 11 Python
Python实现SMTP邮件发送
Jun 16 Python
python接口自动化之ConfigParser配置文件的使用详解
Aug 03 Python
python利用 keyboard 库记录键盘事件
Oct 16 Python
Flask-SocketIO服务端安装及使用代码示例
Nov 26 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 用数组降低程序的时间复杂度
2009/12/04 PHP
PHP回调函数概念与用法实例分析
2017/11/03 PHP
精心挑选的15个jQuery下拉菜单制作教程
2012/06/15 Javascript
Js 冒泡事件阻止实现代码
2013/01/27 Javascript
HTML Color Picker(js拾色器效果)
2013/08/27 Javascript
Js参数值中含有单引号或双引号问题的解决方法
2013/11/06 Javascript
jQuery过滤选择器:not()方法使用介绍
2014/04/20 Javascript
老生常谈javascript变量的命名规范和注释
2016/09/29 Javascript
Js apply方法详解
2017/02/16 Javascript
Vue.js2.0中的变化小结
2017/10/24 Javascript
利用百度地图API获取当前位置信息的实例
2017/11/06 Javascript
微信小程序学习总结(四)事件与冒泡实例分析
2020/06/04 Javascript
Vue Render函数原理及代码实例解析
2020/07/30 Javascript
[10:24]郎朗助力完美“圣”典,天籁交织奏响序曲
2016/12/18 DOTA
详解Python中的循环语句的用法
2015/04/09 Python
基于Python如何使用AIML搭建聊天机器人
2016/01/27 Python
书单|人生苦短,你还不用python!
2017/12/29 Python
python创建文件备份的脚本
2018/09/11 Python
关于python字符串方法分类详解
2019/08/20 Python
Python编程快速上手——选择性拷贝操作案例分析
2020/02/28 Python
如何在windows下安装Pycham2020软件(方法步骤详解)
2020/05/03 Python
python 实现PIL模块在图片画线写字
2020/05/16 Python
Python基于traceback模块获取异常信息
2020/07/23 Python
python连接手机自动搜集蚂蚁森林能量的实现代码
2021/02/24 Python
html5拍照功能实现代码(htm5上传文件)
2013/12/11 HTML / CSS
解决html5中video标签无法播放mp4问题的办法
2017/05/07 HTML / CSS
粉红色的鲸鱼:Vineyard Vines
2018/02/17 全球购物
汽车专业人才自我鉴定范文
2013/12/29 职场文书
2014年党支部承诺书
2014/05/30 职场文书
写字楼租赁意向书
2014/07/30 职场文书
退学证明范本3篇
2014/10/29 职场文书
信访工作个人总结
2015/03/03 职场文书
朋友聚会祝酒词
2015/08/10 职场文书
python实现语音常用度量方法的代码详解
2021/05/25 Python
Vue vee-validate插件的简单使用
2021/06/22 Vue.js
关于PHP数组迭代器的使用方法实例
2021/11/17 PHP