文章目录
描述:
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1 ≤ n ≤ 100
输入描述: 一组字符串
输出描述:如果符合要求输出:OK;否则输出:NG
示例1
> 输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
> 输出:
OK
NG
NG
OK
Python3
while 1:
try:
str = input()
a, b, c, d = 0, 0, 0, 0 # 数字、小写字母、大写字母、其他
flag = 1 # 子串重复
for ii in str:
if ii.isdigit(): a = 1
elif ii.islower(): b = 1
elif ii.isupper(): c = 1
else: d = 1
# 每三个字符进行迭代判断,在整个字符串中,是否存在连续三个字符与当前三个字符完全相同(子串重复)
for jj in range(len(str) - 3):
if str.count(str[jj: jj+3]) > 1:
flag = 0
if len(str) > 8 and a+b+c+d >= 3 and flag:
print('OK')
else:
print('NG')
except:
break
'''
> 输入:
021Abc9000
021Abc9Abc1 # 错误:子串重复
021ABC9000 # 错误:密码类型只有两种
021$bc9000
> 输出:
OK
NG
NG
OK
'''
知识点详解
1、input():获取控制台(任意形式)的输入。输出均为字符串类型。
str1 = input()
print(str1)
print('提示语句:', str1)
print(type(str1))
'''
asd123!#
提示语句: asd123!#
<class 'str'>
'''
常用的强转类型 | 说明 |
---|---|
int(input()) | 强转为整型(输入必须时整型) |
list(input()) | 强转为列表(输入可以是任意类型) |
1.1、input()与list(input())的区别、及其相互转换方法
- 相同点:两个方法都可以进行for循环迭代提取字符,提取后都为字符串类型。
- 不同点:
str = list(input())
将输入字符串转换为list类型,可以进行相关操作。如:str.append()
- 将列表转换为字符串:
str_list = ['A', 'aA', 2.0, '', 1]
- 方法一:
print(''.join(str))
- 方法二:
print(''.join(map(str, str_list)))
备注:若list中包含数字,则不能直接转化成字符串,否则系统报错。
- 方法一:
print(''.join([str(ii) for ii in str_list]))
- 方法二:
print(''.join(map(str, str_list)))
map():根据给定函数对指定序列进行映射。即把传入函数依次作用到序列的每一个元素,并返回新的序列。
(1) 举例说明:若list中包含数字,则不能直接转化成字符串,否则系统报错。
str = ['25', 'd', 19, 10]
print(' '.join(str))
'''
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/test.py", line 188, in <module>
print(' '.join(str))
TypeError: sequence item 3: expected str instance, int found
'''
(2)举例说明:若list中包含数字,将list中的所有元素转换为字符串。
str_list = ['A', 'aA', 2.0, '', 1]
print(''.join(str(ii) for ii in str_list))
print(''.join([str(ii) for ii in str_list]))
print(''.join(map(str, str_list))) # map():根据给定函数对指定序列进行映射。即把传入函数依次作用到序列的每一个元素,并返回新的序列。
'''
AaA2.01
AaA2.01
AaA2.01
'''
2、print() :打印输出。
x, y = 1, 9
print('{},{}' .format(x, y))
print(x, ',', y)
'''
1,9
1 , 9
'''
3、range() :输出指定范围内的所有值(前闭后开)。
函数说明:
range(start=0,stop,step=1)
输入参数:
- start:序列的起始点,默认值0。
- stop:序列的终止点。
- step:滑动步长,默认为1。正数表示迭代上升,负数表示迭代下降。
备注:前闭后开[A,B),即可以取到A值,但取不到B值。
# (1)指定stop=9, start默认为0,step默认为1
for i in range(9):
print('A =', i)
'''
A = 0
A = 1
A = 2
A = 3
A = 4
A = 5
A = 6
A = 7
A = 8
'''
# (2)指定start=2, stop=9, step默认为1
for i in range(2, 9):
print('B =', i)
'''
B = 2
B = 3
B = 4
B = 5
B = 6
B = 7
B = 8
'''
# (3)指定start=2, stop=9, step=-2
for i in range(9, 2, -2):
print('C =', i)
'''
C = 9
C = 7
C = 5
C = 3
'''
4、len():返回字符串、列表、字典、元组等的长度。
函数说明:
len(object)
str_temp = "Hello, boy !"
print(len(str_temp)) # 【输出结果】12
#############################################
list_temp = ['h', 'e', 'l', 'l', 'o']
print(len(list_temp)) # 【输出结果】5
#############################################
dict_temp = {'num': 520, 'name': "do do"}
print(len(dict_temp)) # 【输出结果】2
#############################################
tuple_temp = ('G', 'o', 'o', 'd')
print(len(tuple_temp)) # 【输出结果】4
5、count():统计字符串里某个字符出现的次数。可以选择字符串索引的起始位置和结束位置。
函数说明:
str.count("char", start, end)
或str.count("char")
参数说明:
- str:为要统计的字符(可以是单字符,也可以是多字符)。
- start:为索引字符串的起始位置,默认参数为0。
- end:为索引字符串的结束位置,默认参数为字符串长度即len(str)。
str_temp = "i love python, i am learning python"
print(str_temp.count("i")) # 【输出结果】3 # 单字符统计:start和end为默认参数
print(str_temp.count("i", 2)) # 【输出结果】2 # 单字符统计:star=2,end值为默认参数
print(str_temp.count("i", 0, 5)) # 【输出结果】1 # 单字符统计:star=2,end=5
################################################################################################
print(str_temp.count("py")) # 【输出结果】2 # 多字符统计:start和end为默认参数
6、str.isdigit(),str.isnumeric(),str.isdecimal():检查字符串是否只包含十进制字符,返回布尔值。
方法 | Unicode数字 | 全角数字(双字节) | 罗马数字 | 汉字数字 | byte数字(单字节) | 浮点数 | 负数 | 科学计数法 | 二进制 | 八进制 | 十六进制 |
---|---|---|---|---|---|---|---|---|---|---|---|
isdecimal() | True | True | False | False | Error | False | False | False | False | False | False |
isdigit() | True | True | False | False | True | False | False | False | False | False | False |
isnumeric() | True | True | True | True | Error | False | False | False | False | False | False |
str = '123'
print(str.isdecimal())
print(str.isdigit())
print(str.isnumeric())
'''
True
True
True
'''
7、str.isupper(),str.islower(),str.istitle():检测字符串的大小写,返回布尔值。
- (1)
str.isupper()
:判断字符串中,含有字母的部分是否全大写。- (2)
str.islower()
:判断字符串中,含有字母的部分是否全小写。- (3)
str.istitle()
:检测字符串中,所有单词拼写的首字母是否为大写,且其他字母为小写。
str = '123aaa'
print(str.islower())
print(str.isupper())
str = 'This Is All'
print(str.istitle())
str = 'This Is ALL'
print(str.istitle())
'''
True
False
True
False
'''