题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import sys
def check_password(password):
if len(password) <= 8:
return False
has_upper, has_lower, has_number, has_signal = False, False, False, False
lower_list = "qwertyuiopasdfghjklzxcvbnm"
upper_list = "QWERTYUIOPASDFGHJKLZXCVBNM"
number_list = "1234567890"
for i in range(len(password)):
has_lower = True
elif char in upper_list:
has_upper = True
elif char in number_list:
has_number = True
else:
has_signal = True
# 检查j的值
if i + 1 >= len(password):
if password[i:i+3] == password[j:j+3]:
return False
if sum([has_lower, has_upper, has_number, has_signal]) < 3:
return False
return True
for line in sys.stdin:
password = line.strip()
if check_password(password):
print("OK")
else:
print("NG")
def check_password(password):
if len(password) <= 8:
return False
has_upper, has_lower, has_number, has_signal = False, False, False, False
lower_list = "qwertyuiopasdfghjklzxcvbnm"
upper_list = "QWERTYUIOPASDFGHJKLZXCVBNM"
number_list = "1234567890"
for i in range(len(password)):
char = password[i]
# 检查是否符合四个条件中的三个
if char in lower_list:has_lower = True
elif char in upper_list:
has_upper = True
elif char in number_list:
has_number = True
else:
has_signal = True
# 检查j的值
if i + 1 >= len(password):
break
# 检查是否重复
for j in range(i + 1, len(password)): if password[i:i+3] == password[j:j+3]:
return False
if sum([has_lower, has_upper, has_number, has_signal]) < 3:
return False
return True
for line in sys.stdin:
password = line.strip()
if check_password(password):
print("OK")
else:
print("NG")