题解 | 密码强度检查

密码强度检查

https://www.nowcoder.com/practice/40cc4cfe4a7d45839393fc305fc5609e

# 接收来自命令行输入的数据
def receive_strings():
    num = int(input())
    # 创建一个字典动态存储输入的字符串
    strings_dict = {}

    # 读取字符串输入
    for i in range(num):
        strings_dict[f"string_{i+1}"] = input("")

    # 计算输入的字符串的长度
    li = []
    result = []
    for key, value in strings_dict.items():
        length = len(value)
        li.append(length)

    # 计算输入的字符串包含多少种字符
    def count_char_types(value):
        # 初始化各类计数器
        digit = False  # 数字
        upper = False  # 大写字母
        lower = False  # 小写字母
        special = False  # 特殊字符
        count = 0
        for char in value:
            if char.isdigit():
                digit = True
            elif char.isupper():
                upper = True
            elif char.islower():
                lower = True
            else:
                if not char.isspace():
                    special = True

        # 统计包含的类型数量
        if digit:
            count += 1
        if upper:
            count += 1
        if lower:
            count += 1
        if special:
            count += 1
        return count

    # 对每个字符串调用 count_char_types 函数
    for value in strings_dict.values():
        char_type_count = count_char_types(value)
        result.append(char_type_count)

    # 返回三个变量
    return strings_dict, li, result


# 判定密码强度
def password_adjust(li, result):
    # 逐个判断每个密码强度
    for i in range(len(li)):
        length = li[i]
        char_types = result[i]

        if length < 8:
            print("Weak")
        else:
            if char_types == 4:
                print("Strong")
            elif char_types == 3:
                print("Medium")
            else:
                print("Weak")


def main():
    strings_dict, li, result = receive_strings()
    password_adjust(li, result)


# 运行主程序
if __name__ == "__main__":
    main()

用的Python语言,首先就是对输入的多个密码循环的创建变量名,这里用了字典的键值对形式存储strings_dict[f"string_{i+1}"] = input("");列表li用来存储密码的长度,result存储密码包含多少种字符。

最后一步是判定密码强度,这个地方处理不当,很容易循环嵌套,对每个字符串长度和包含字符种类循环判断一次;

使用主函数main直接调用receive_strings()和password_adjust()函数

全部评论

相关推荐

09-16 14:43
已编辑
华南农业大学 游戏后端
背景&nbsp;双一流本硕&nbsp;双非大圆满&nbsp;只找游戏开发相关的岗位。&nbsp;8&nbsp;月初开始秋招到现在&nbsp;投了四五十家吧,&nbsp;目前两&nbsp;offer,&nbsp;不打算继续投了,把剩下的流程走完就开始沉淀了。目前两&nbsp;offer&nbsp;一个是网易互娱测开&nbsp;base&nbsp;广州,一个是江娱互动客户端开发&nbsp;base&nbsp;北京。应该确定网易这个了,说实话北京这个我挺想去的,这家的产品和工作氛围我了解了也不错,是那种踏实做事的,可惜我是广东人。网易的测开是调剂的二志愿,看了下有内部转岗机会,所以打算后面找个时间提前实习,沉淀下再做一个&nbsp;demo&nbsp;作品,写一些&nbsp;shader,增强下图形学渲染的能力,再学点编辑器开发。看到时候内部转岗或者春招继续投客户端开发这样。后面还能再动摇的话应该就灵犀或者腾子了吧(假如这两家确认的是客户端开发岗的话)。-----------------------补下timeline网易互娱&nbsp;测开&nbsp;8.2笔试&nbsp;&nbsp;8.21&nbsp;技术面&nbsp;&nbsp;8.29&nbsp;leader&amp;HRBP面(终面)&nbsp;9.8&nbsp;录用审核(之前一直显示面试中)9.14&nbsp;oc江娱互动&nbsp;客户端开发&nbsp;8.29主程面&nbsp;9.3&nbsp;制作人面&nbsp;9.5&nbsp;BOSS面&nbsp;9.11&nbsp;口头OC&nbsp;9.15&nbsp;正式offer后面考虑了一下&nbsp;&nbsp;感觉还是能走开发就开发吧,测开不太感兴趣,要内部活水转岗还要满1年才能申请。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务