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和结果文件名。


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

相关推荐
TomEval2 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
prince_zxill2 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
ShineWinsu3 小时前
对于Qt:QFormLayout常见属性以及方法的解析
开发语言·c++·qt·面试·客户端·表单·qformlayout
Thanks_ks4 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref
CyrusYiVPP4 小时前
实测 uv 0.13.7:同一套包,pip 装 94 秒,它只用 19 秒
人工智能·python
yiangL_4 小时前
Python 学习笔记:递归、turtle绘图、字符串与函数
笔记·python·学习
lula8886 小时前
PVC-U、PVC-C、PVC-O 差一个字母,差的是三种增强路线
python
数字工作台7 小时前
给 LangGraph 加一道可审计记忆:替换默认 Checkpointer,实现防篡改 + 崩溃不丢数据
python·agent
DKPT7 小时前
Python 的 Web 框架:选型选错的人,都踩了同一个坑
python
2401_868534788 小时前
逻辑网络设计
网络·python