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的另外几种语言实现
Jan 29 Python
Python和C/C++交互的几种方法总结
May 11 Python
Python+matplotlib实现填充螺旋实例
Jan 15 Python
python链接oracle数据库以及数据库的增删改查实例
Jan 30 Python
使用Python爬了4400条淘宝商品数据,竟发现了这些“潜规则”
Mar 23 Python
详解Python中的四种队列
May 21 Python
django框架防止XSS注入的方法分析
Jun 21 Python
使用python获取邮箱邮件的设置方法
Sep 20 Python
Python3自定义http/https请求拦截mitmproxy脚本实例
May 11 Python
PyQt5的QWebEngineView使用示例
Oct 20 Python
浅析python连接数据库的重要事项
Feb 22 Python
python 安全地删除列表元素的方法
Mar 16 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
AM/FM收音机的安装与调试
2021/03/02 无线电
PHP Streams(流)详细介绍及使用
2015/05/12 PHP
PHP反射机制原理与用法详解
2017/02/15 PHP
PHP实现执行外部程序的方法详解
2017/08/17 PHP
用js来获取上传的文件名纯粹是为了美化而用
2013/10/23 Javascript
JavaScript实现的一个日期格式化函数分享
2014/12/06 Javascript
jQuery 中DOM 操作详解
2015/01/13 Javascript
七夕情人节丘比特射箭小游戏
2015/08/20 Javascript
JavaScript运行过程中的“预编译阶段”和“执行阶段”
2015/12/16 Javascript
javascript仿京东导航左侧分类导航下拉菜单效果
2020/11/25 Javascript
JavaScript编写页面半透明遮罩效果的简单示例
2016/05/09 Javascript
关于input全选反选恶心的异常情况
2016/07/24 Javascript
JSON与String互转的实现方法(Javascript)
2016/09/27 Javascript
AngularJS入门教程之与服务器(Ajax)交互操作示例【附完整demo源码下载】
2016/11/02 Javascript
Angular-Touch库用法示例
2016/12/22 Javascript
JavaScript正则表达式简单实用实例
2017/06/23 Javascript
解决webpack -p压缩打包react报语法错误的方法
2017/07/03 Javascript
基于VUE移动音乐WEBAPP跨域请求失败的解决方法
2018/01/16 Javascript
微信小程序实现topBar底部选择栏效果
2018/07/20 Javascript
vue-router 手势滑动触发返回功能
2018/09/30 Javascript
layui 数据表格 根据值(1=业务,2=机构)显示中文名称示例
2019/10/26 Javascript
详解微信小程序之提高应用速度小技巧
2020/01/07 Javascript
vue 实现setInterval 创建和销毁实例
2020/07/21 Javascript
小程序实现简单语音聊天的示例代码
2020/07/24 Javascript
[55:44]OG vs NAVI 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
windows下安装python paramiko模块的代码
2013/02/10 Python
python中from module import * 的一个坑
2014/07/20 Python
浅谈python numpy中nonzero()的用法
2018/04/02 Python
Python中创建二维数组
2018/10/17 Python
python对csv文件追加写入列的方法
2019/08/01 Python
美国高级工作服品牌:Carhartt
2018/01/25 全球购物
FC-Moto美国:欧洲最大的摩托车服装和头盔商店之一
2019/08/24 全球购物
餐饮管理自我介绍信
2014/01/15 职场文书
医药营销个人求职信范文
2014/02/07 职场文书
校长四风对照检查材料
2014/09/27 职场文书
美元符号 $
2022/02/17 杂记