for example ACM An algorithmic problem of password qualification verification under Mode :
subject :
Password requirements :
1. The length exceeds 8 position
2. Include upper and lower case letters . Numbers . Other symbols , At least three of the above four
3. Cannot have length greater than 2 The substring containing the common element of is repeated ( notes : Other symbols do not contain spaces or line breaks )
Data range : The length of the input string meets the requirements 1 \le n \le 100 \1≤n≤100
Input description :
A set of strings .
Output description :
If it meets the requirements, output :OK, Otherwise output NG
Example :
Input
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
Output
OK
NG
NG
OK
Code :
def check(s):
if len(s) <= 8:
return 0
a, b, c, d = 0, 0, 0, 0
for item in s:
if ord('a') <= ord(item) <= ord('z'):
a = 1
elif ord('A') <= ord(item) <= ord('Z'):
b = 1
elif ord('0') <= ord(item) <= ord('9'):
c = 1
else:
d = 1
if a + b + c + d < 3:
return 0
for i in range(len(s)-3): # Check whether there are repeated substrings with more than three characters
if len(s.split(s[i:i+3])) >= 3: # The substring repetition judgment is performed here
return 0
return 1
while 1:
try:
print('OK' if check(input()) else 'NG')
except:
break
analysis :s.split(s[i:i+3]) Good use .
021Abc9Abc1, The string is passed through s.split(s[i:i+3]) After treatment: [‘021’, ‘9’, ‘1’], Therefore, there are repeated substrings with more than three characters .
Be careful : Empathy , Input 021AbcAbc1,[‘021’, ‘’, ‘1’], You can also check out repeated substrings with more than three characters .