题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import sys
symbols = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
def grade_pwd(s: str) -> str:
# 对长度评分
if len(s) <= 4:
length_score = 5
elif len(s) <=7:
length_score = 10
else:
length_score = 25
has_upper = has_lower = has_number = has_symbol = False
number_score = symbol_score = 0
for c in s:
# 对数字进行评分
if c.isdigit():
if not has_number:
has_number = True
number_score = 10
else:
number_score = 20
# 对字符进行评分
elif c in symbols:
if not has_symbol:
has_symbol = True
symbol_score = 10
else:
symbol_score = 25
elif c.isupper():
has_upper = True
elif c.islower():
has_lower = True
# 对字母进行评分
letter_score = 0
if has_upper and has_lower:
letter_score = 20
elif has_upper or has_lower:
letter_score = 10
additional_score = 0 # 奖励分
if has_number and letter_score:
additional_score = 2
if has_symbol and additional_score:
additional_score = 3
if letter_score == 20 and additional_score == 3:
additional_score = 5
score = length_score + letter_score + number_score + symbol_score + additional_score
if score >= 90:
return 'VERY_SECURE'
if score >= 80:
return 'SECURE'
if score >= 70:
return 'VERY_STRONG'
if score >= 60:
return 'STRONG'
if score >= 50:
return 'AVERAGE'
if score >= 25:
return 'WEAK'
else:
return 'VERY_WEAK'
for pwd in sys.stdin:
print(grade_pwd(pwd))