题解 | 密码验证合格程序

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

# 注意:示例1的第5个密码实际为非法密码,示例1对第5个密码的输出判断有误
import sys

s_list = []    # 将数量未知的输入存储为列表
for i in sys.stdin:
    s_list.append(i.strip())

def count(s):    # 将一段密码放入验证字符种类
    a, A, b, c = 0, 0, 0, 0
    count = 0
    for i in s:
        if ord(i) >= 65 and ord(i) <= 90:
            A = 1
        elif ord(i) >= 97 and ord(i) <= 122:
            a = 1
        elif ord(i) >= 48 and ord(i) <= 57:
            b = 1
        else:
            c = 1
    count = a + A + b + c
    if count >= 3:
        return 1    # 1为合法
    else:
        return 0

def cut(s):    # 将一段密码放入后切片判断子串
    list1 = []
    dict1 = {}
    count = 1
    for i in range(len(s)):    # 获得所有长度大于2的字符串
        for j in range(i + 3, len(s) + 1):
            list1.append(s[i:j])
    for i in list1:    # 统计各字符串出现次数
        if i in dict1:
            dict1[i] += 1
        else:
            dict1[i] = 1
    for i in dict1.values():    # 若出现次数大于等于2则不合法
        if i >= 2:
            count = 0
            break
    return count

for i in s_list:
    if len(i) >= 8 and count(i) == 1 and cut(i) == 1:
        print('OK')
    else:
        print('NG')

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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