题解 | #密码强度等级#

密码强度等级

http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361

方法1:板砖比较简洁的解析。python

while True:
    try:
        str_data = input().strip()
        num, up_char, low_char, other, score = 0, 0, 0, 0, 0

        for data in str_data:
            if data.isdigit():
                num += 1
            elif data.isalpha():
                if data.lower() == data:
                    low_char += 1
                else:
                    up_char += 1
            else:
                other += 1
        if len(str_data) < 5:
            score += 5
        elif len(str_data) < 8:
            score += 10
        else:
            score += 25
        if up_char==0 and low_char==0:
            pass
        elif (up_char==0 and low_char!=0) or (up_char!=0 and low_char==0):
            score += 10
        else:
            score += 20
        if num == 0:
            pass
        elif num == 1:
            score += 10
        else:
            score += 20
        if other == 0:
            pass
        elif other == 1:
            score += 10
        else:
            score += 25
        if num != 0 and (up_char+low_char) != 0 and other==0:
            score += 2
        elif num != 0 and up_char != 0 and low_char != 0 and other!=0:
            score += 5
        elif num != 0 and (up_char+low_char) != 0 and other!=0:
            score += 3
        if score >=90:
            print('VERY_SECURE')
        elif score >=80:
            print('SECURE')
        elif score >= 70:
            print('VERY_STRONG')
        elif score >= 60:
            print('STRONG')
        elif score >= 50:
            print('AVERAGE')
        elif  score>=25:
            print('WEAK')
        else:
            print('VERY_WEAK')
    except:
        break

方法2:python

#python3:一直不对,暂时查不出问题在哪#
while True:
    try:
        s=input()
        score=0
        digit_num=0#密码中数字的个数
        upper_num=0#密码中大写字母的个数
        lower_num=0#密码中小写字母的个数
        index_num=0#密码中符号的个数
        for i in s:#遍历密码,确定上述各项的值
            if i.isdigit():
                digit_num+=1
            elif i.isupper():
                upper_num+=1
            elif i.islower():
                lower_num+=1
            else:
                index_num+=1
        n=len(s)
        if n<=4:score+=5#密码长度小于等于4,分值加5
        elif n<=7:score+=10#密码长度大于等于5小于等于7,分值加10
        else: score+=25#密码长度大于等于8,分值加25
        if digit_num==0:#密码中数字个数为0,分值加0
            score+=0
        elif digit_num==1:#密码中数字个数为1,分值加10
            score+=10
        else:#密码中数字个数大于1,分值加20
            score+=20
        if index_num==0:#密码中符号个数为0,分值加0
            score+=0
        elif index_num==1:#密码中符号个数为1,分值加10
            score+=10
        else:#密码中符号个数大于1,分值加25
            score+=25
        if upper_num==0 and lower_num==0:#密码中大小写字母个数都为0,分值加0
            score+=0
        #elif upper_num==n or lower_num==n:#密码中大(小)写字母个数为n(密码长度),分值加10
        #    score+=10
        #elif upper_num!=0 and lower_num!=0 and ((upper_num+lower_num)==n):#密码为大小写混合字母,分值加20
        #    score+=20
        elif upper_num==0 or lower_num==0:#密码中的字母都为大(小)写字母,分值加10(这里应该不要求密码全是字母————但还是不对,问题到底在哪?)
            score+=10
        else: 
            score+=20#密码中的大(小)写字母混合,分值加20
        if digit_num!=0 and (upper_num!=0 or lower_num!=0):#密码中有数字和字母,分值加2
            score+=2
        elif digit_num!=0 and (upper_num!=0 or lower_num!=0) and index_num!=0:#密码中有数字、字母和符号,分值加3
            score+=3
        elif upper_num!=0 and lower_num!=0 and digit_num!=0 and index_num!=0:#密码中有数字、大小写字母和符号,分值加5
            score+=5
        if score >= 90:
            print('VERY_SECURE')
        elif score >= 80:
            print('SECURE')
        elif score >= 70:
            print('VERY_STRONG')
        elif score >= 60:
            print('STRONG')
        elif score >= 50:
            print('AVERAGE')
        elif score >= 25:
            print('WEAK')
        else:
            print('VERY_WEAK')
    except:
        break
全部评论

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
object3:开始给部分🌸孝子上人生第一课了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务