isdigit( ), islower( ), isupper( ), isalpha( ) , isspace( )这几个函数在算法题里面还是很有用处的。

isdigit( )  检测字符串是否只由数字组成。  和 isnumeric( )函数类似

islower( )   检测字符串是否由小写字母组组成

isupper( )  检测字符串中所有的字母是否都为大写

isalpha( )  检测字符串是否只由字母组成

isspace( )  检测字符串是否只由空格组成

题目:

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复

说明:长度超过2的子串

如果符合要求输出:OK,否则输出NG

输入:021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000

输出:OK NG NG OK

import sys

try:
    # 大小写,字母,
    def panchar(sss):
        standard = [0] * 4
        for i in sss:
            # print(i)
            # 0
            # 2
            # 1
            # A
            # b
            # print(len(sss))
            # 数字
            if i.isdigit():
                standard[0] = 1
                # print(i.isdigit())
            # 小写
            if i.islower():
                standard[1] = 1
            # 大写
            if i.isupper():
                standard[2] = 1
            # 全都是字母,数字,空格
            if not i.isalpha() and not i.isdigit() and not i.isspace():
                standard[3] = 1
            if sum(standard) >= 3:
                return False
        return True

    # 不能有相同长度超 2 的字串重复
    def zichuan(sss):
        for i in range(len(sss) - 3):
            zichuan_1 = sss[i: i + 3]
            zichuan_2 = sss[i + 1::]
            if zichuan_1 in zichuan_2:
                return True
        return False

    result = []
    while True:
        line = sys.stdin.readline().strip()
        if line == '':
            break
        if len(line) <= 8:
            result.append('NG')
        # 大小写字母.数字.其它符号
        elif panchar(line):
            result.append('NG')
        elif zichuan(line):
            result.append('NG')
        else:
            result.append('OK')
    for i in result:
        print(i)
except:
    pass

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐