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处理cookie详解
Feb 07 Python
python编程通过蒙特卡洛法计算定积分详解
Dec 13 Python
安装python3的时候就是输入python3死活没有反应的解决方法
Jan 24 Python
点球小游戏python脚本
May 22 Python
python适合人工智能的理由和优势
Jun 28 Python
使用pandas读取文件的实现
Jul 31 Python
python 两个数据库postgresql对比
Oct 21 Python
Window系统下Python如何安装OpenCV库
Mar 05 Python
python IDLE添加行号显示教程
Apr 25 Python
Python实现电视里的5毛特效实例代码详解
May 15 Python
在tensorflow以及keras安装目录查询操作(windows下)
Jun 19 Python
Python 3.10 的首个 PEP 诞生,内置类型 zip() 迎来新特性(推荐)
Jul 03 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/26 PHP
PHP中addslashes与mysql_escape_string的区别分析
2016/04/25 PHP
php时间戳转换代码详解
2019/08/04 PHP
Ext JS Grid在IE6 下宽度的问题解决方法
2009/02/15 Javascript
Web 前端设计模式--Dom重构 提高显示性能
2010/10/22 Javascript
JS获取url链接字符串 location.href
2013/12/23 Javascript
15个jquery常用方法、小技巧分享
2015/01/13 Javascript
javascript实用方法总结
2015/02/06 Javascript
js中substr,substring,indexOf,lastIndexOf,split,replace的用法详解
2015/11/09 Javascript
HTML页面,测试JS对C函数的调用简单实例
2016/08/09 Javascript
Angular.js跨controller实现参数传递的两种方法
2017/02/20 Javascript
JS简单获取当前日期时间的方法(如:2017-03-29 11:41:10 星期四)
2017/03/29 Javascript
JavaScript中Require调用js的实例分享
2017/10/27 Javascript
如何将HTML字符转换为DOM节点并动态添加到文档中详解
2018/08/19 Javascript
Vue模拟数据,实现路由进入商品详情页面的示例
2018/08/31 Javascript
Angular ui-roter 和AngularJS 通过 ocLazyLoad 实现动态(懒)加载模块和依赖
2018/11/25 Javascript
微信小程序webview 脚手架使用详解
2019/07/22 Javascript
微信小程序 下拉刷新及上拉加载原理解析
2019/11/06 Javascript
JS实现电商商品展示放大镜特效
2020/01/07 Javascript
详解React 元素渲染
2020/07/07 Javascript
vue 解决在微信内置浏览器中调用支付宝支付的情况
2020/11/09 Javascript
Python实现改变与矩形橡胶的线条的颜色代码示例
2018/01/05 Python
python事件驱动event实现详解
2018/11/21 Python
使用python读取.text文件特定行的数据方法
2019/01/28 Python
python实现月食效果实例代码
2019/06/18 Python
Python Opencv提取图片中某种颜色组成的图形的方法
2019/09/19 Python
Python彻底删除文件夹及其子文件方式
2019/12/23 Python
印尼美容产品购物网站:PerfectBeauty.id
2017/12/01 全球购物
香港连卡佛百货官网:Lane Crawford
2019/09/04 全球购物
Java基础知识面试要点
2016/07/29 面试题
应届毕业生个人自荐信范文
2013/11/30 职场文书
四年级学生期末评语
2014/12/26 职场文书
党员电教片《信仰》心得体会
2016/01/15 职场文书
Pytorch反向传播中的细节-计算梯度时的默认累加操作
2021/06/05 Python
Python 数据科学 Matplotlib图库详解
2021/07/07 Python
win11高清晰音频管理器在哪里?win11找不到高清晰音频管理器解决办法
2022/04/08 数码科技