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通过函数属性实现全局变量的方法
May 16 Python
Tensorflow中的placeholder和feed_dict的使用
Jul 09 Python
Django 登陆验证码和中间件的实现
Aug 17 Python
python地震数据可视化详解
Jun 18 Python
Python 实现交换矩阵的行示例
Jun 26 Python
详解PANDAS 数据合并与重塑(join/merge篇)
Jul 09 Python
python批量图片处理简单示例
Aug 06 Python
Python函数装饰器原理与用法详解
Aug 16 Python
详解python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决
Aug 27 Python
Python的Lambda函数用法详解
Sep 03 Python
PyQT5 emit 和 connect的用法详解
Dec 13 Python
Python 将 QQ 好友头像生成祝福语的实现代码
May 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
3
2006/10/09 PHP
火车头采集器3.0采集图文教程
2007/03/17 PHP
php set_time_limit()函数的使用详解
2013/06/05 PHP
php中mkdir函数用法实例分析
2014/11/15 PHP
php事件驱动化设计详解
2016/11/10 PHP
PHP基于堆栈实现的高级计算器功能示例
2017/09/15 PHP
在laravel5.2中实现点击用户头像更改头像的方法
2019/10/14 PHP
jQuery实现炫酷的鼠标轨迹特效
2015/02/01 Javascript
高性能JavaScript模板引擎实现原理详解
2015/02/05 Javascript
js全选实现和判断是否有复选框选中的方法
2015/02/17 Javascript
jQuery实现延迟跳转的方法
2015/06/05 Javascript
jQuery实现仿腾讯迷你首页选项卡效果代码
2015/09/17 Javascript
jQuery实现的给图片点赞+1动画效果(附在线演示及demo源码下载)
2015/12/31 Javascript
Hallo.js基于jQuery UI所见即所得的Web编辑器
2016/01/26 Javascript
让微信小程序支持ES6中Promise特性的方法详解
2017/06/13 Javascript
使用requirejs模块化开发多页面一个入口js的使用方式
2017/06/14 Javascript
AngularJS实现进度条功能示例
2017/07/05 Javascript
基于BootStrap multiselect.js实现的下拉框联动效果
2017/07/28 Javascript
通过实例了解JS执行上下文运行原理
2020/06/17 Javascript
vue - props 声明数组和对象操作
2020/07/30 Javascript
vue+高德地图实现地图搜索及点击定位操作
2020/09/09 Javascript
Vue select 绑定动态变量的实例讲解
2020/10/22 Javascript
python操作cfg配置文件方式
2019/12/22 Python
python系统指定文件的查找只输出目录下所有文件及文件夹
2020/01/19 Python
Move Free官方海外旗舰店:美国骨关节健康专业品牌
2017/12/06 全球购物
Java程序员面试题
2013/07/15 面试题
毕业自我鉴定
2013/11/05 职场文书
产品工艺师的岗位职责
2013/11/15 职场文书
公务员政审单位鉴定材料
2014/05/16 职场文书
运动会拉拉队口号
2014/06/09 职场文书
2014法院四风问题对照检查材料思想汇报
2014/10/04 职场文书
2015年专项整治工作总结
2015/04/03 职场文书
2015年教研组工作总结
2015/05/04 职场文书
解约证明模板
2015/06/19 职场文书
Python基础之变量的相关知识总结
2021/06/23 Python
Linux中文件的基本属性介绍
2022/06/01 Servers