题解 | #HJ20 密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import sys lower_letter = "".join([chr(ord("a") + i) for i in range(26)]) upper_letter = "".join([chr(ord("A") + i) for i in range(26)]) num_string = "0123456789" def verify_pwd(pwd): pwd = pwd.strip() # 校验规则1 if len(pwd) < 8: return "NG" # 校验规则2 count = 0 flag_1 = flag_2 = flag_3 = flag_4 = False for letter in pwd: if not flag_1 and letter in lower_letter: count += 1 flag_1 = True if not flag_2 and letter in upper_letter: count += 1 flag_2 = True if not flag_3 and letter in num_string: count += 1 flag_3 = True if ( not flag_4 and letter not in lower_letter and letter not in upper_letter and letter not in num_string ): count += 1 flag_4 = True if count < 3: return "NG" # 校验规则3 for i in range(3, len(pwd) // 2 + 1): for j in range(0, len(pwd) - i + 1): length = pwd.count(pwd[j:j+i]) if length > 1: return "NG" return "OK" for line in sys.stdin: result = verify_pwd(line.strip()) print(result)