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


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

相关推荐
一般清意味……5 分钟前
快速上手C语言【上】(非常详细!!!)
c语言·开发语言
卑微求AC6 分钟前
(C语言贪吃蛇)16.贪吃蛇食物位置随机(完结撒花)
linux·c语言·开发语言·嵌入式·c语言贪吃蛇
技术无疆16 分钟前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
羊小猪~~24 分钟前
机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
人工智能·python·机器学习·数据挖掘·数据分析·回归·时序数据库
金灰32 分钟前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
爱上语文36 分钟前
Java LeetCode每日一题
java·开发语言·leetcode
qq_2739002336 分钟前
解析TMalign文本文件中的转换矩阵
python·生物信息学
Манго нектар1 小时前
JavaScript for循环语句
开发语言·前端·javascript
程序猿小D1 小时前
第二百六十九节 JPA教程 - JPA查询OrderBy两个属性示例
java·开发语言·数据库·windows·jpa
阿华的代码王国1 小时前
【JavaEE】——文件IO的应用
开发语言·python