python破解字母已知但大小写未知密码

python穷举已知字符串中某个或多个字符为大写的所有情况

可以使用递归函数来实现这个功能。以下是一个示例代码:

python 复制代码
def generate_uppercase_combinations(s, index=0, current=''):
    if index == len(s):
        print(current)
        return
    
    generate_uppercase_combinations(s, index + 1, current + s[index])
    
    if s[index].isalpha() and s[index].islower():
        generate_uppercase_combinations(s, index + 1, current + s[index].upper())

# 测试代码
s = "abc"
generate_uppercase_combinations(s)

generate_uppercase_combinations函数接受一个字符串s和两个可选参数index和current。index表示当前处理的字符的索引,current表示当前生成的字符串。函数首先将不改变当前字符大小写继续进行递归调用,然后将当前字符转为大写后进行递归调用。最终会打印出所有可能的情况。

在示例中,给定字符串为"abc",会依次输出:"abc"、"abC"、"aBc"、"aBC"、"Abc"、"AbC"、"ABc"、"ABC"。

把生成的所有结果写入result.txt

python 复制代码
def generate_uppercase_combinations(s, index=0, current='', output_file='result.txt'):
    if index == len(s):
        with open(output_file, 'a') as file:
            file.write(current + '\n')
        return
    
    generate_uppercase_combinations(s, index + 1, current + s[index], output_file)
    
    if s[index].isalpha() and s[index].islower():
        generate_uppercase_combinations(s, index + 1, current + s[index].upper(), output_file)

# 测试代码
s = "abc"
with open('result.txt', 'w') as file:
    file.write('')
generate_uppercase_combinations(s)

output_file参数用于指定结果输出的文件名result.txt

在递归函数中,当生成完一组结果后,将结果写入到文件中。

在测试代码中,先创建或清空result.txt文件,然后调用函数生成结果并写入文件。

最终结果会保存在result.txt文件中。

可以根据需求修改输入字符串s和结果文件名。


我的密码什么时候才能找回来啊😿

相关推荐
李白同学10 分钟前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?1 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
失败尽常态5231 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447741 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农1 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿2 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing2 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
彳卸风2 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead3 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
xinxiyinhe3 小时前
如何设置Cursor中.cursorrules文件
人工智能·python