python 穷举指定长度的密码例子

 更新时间:2020年04月02日 09:22:41   作者:lacoucou  
这篇文章主要介绍了python 穷举指定长度的密码例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

本程序可根据给定的字符字典,穷举指定长度的所有字符串:

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实现PS滤镜特效之扇形变换效果示例

    Python实现PS滤镜特效之扇形变换效果示例

    这篇文章主要介绍了Python实现PS滤镜特效之扇形变换效果,结合实例形式分析了Python实现PS滤镜扇形变换效果的原理与相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • Python实现的金山快盘的签到程序

    Python实现的金山快盘的签到程序

    正在学习python而且自己一直在用金山快盘,所以就写来个签到的功能,每天定时跑
    2013-01-01
  • Python按条件删除Excel表格数据的方法(示例详解)

    Python按条件删除Excel表格数据的方法(示例详解)

    本文介绍基于Python语言,读取Excel表格文件,基于我们给定的规则,对其中的数据加以筛选,将不在指定数据范围内的数据剔除,保留符合我们需要的数据的方法,感兴趣的朋友跟随小编一起看看吧
    2024-08-08
  • Python开发桌面小程序功能

    Python开发桌面小程序功能

    这篇文章主要介绍了Python开发一个桌面小程序功能,开发环境界面设置,功能介绍结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • python如何修改图像的分辨率

    python如何修改图像的分辨率

    这篇文章主要介绍了python如何修改图像的分辨率问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • Python 70行代码实现简单算式计算器解析

    Python 70行代码实现简单算式计算器解析

    这篇文章主要介绍了Python 70行代码实现简单算式计算器解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python写代码的七条重要技巧介绍

    Python写代码的七条重要技巧介绍

    大家好,本篇文章主要讲的是Python写代码的七条重要技巧介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • Python中zip()函数的解释和可视化(实例详解)

    Python中zip()函数的解释和可视化(实例详解)

    zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。这篇文章主要介绍了Python中zip()函数的解释和可视化,需要的朋友可以参考下
    2020-02-02
  • Python循环语句之break与continue的用法

    Python循环语句之break与continue的用法

    这篇文章主要介绍了Python循环语句之break与continue的用法,是Python入门学习中的基础知识,需要的朋友可以参考下
    2015-10-10
  • python中as用法实例分析

    python中as用法实例分析

    这篇文章主要介绍了python中as用法,实例分析了as的功能及相关使用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04

最新评论