程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The wonderful use of Pythons split () method

編輯:Python

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved