需求:
解决方法(本人写的):
写一个与需求相关的正则表达式,断言是否匹配,不匹配返回false,匹配返回ture
代码参考如下
import redef checkio(data): password_one = len(data) >= 10 # 长度大于等于10 password_two = re.findall("[0-9]",data) # 必有一个数字 password_three = re.findall("[a-z]",data) # 必有一个小写字母 password_four = re.findall("[A-Z]",data) # 必有一个大写字母 if password_one and password_two and password_three and password_four: return True else: return False # 断言if __name__ == '__main__': assert checkio('A1213pokl') == False, "1st example" assert checkio('bAse730onE4') == True, "2nd example" assert checkio('asasasasasasasaas') == False, "3rd example" assert checkio('QWERTYqwerty') == False, "4th example" assert checkio('123456123456') == False, "5th example" assert checkio('QwErTy911poqqqq') == True, "6th example" assert checkio("erer798rew9rew9r7ew987rw") == False # 成功的话,就输出这句话 print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
其他解决方法:
参考他人的,这个正则表达式没看懂,有理解的同学麻烦留言一下
import re
def checkio(data): # (?=) is positive lookahead. Use this construction to make sure the enclosed pattern exists return bool(re.search(r'^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{10,}$', data))
if __name__ == '__main__': # These "asserts" using only for self-checking and not necessary for # auto-testing assert checkio('A1213pokl') == False, "1st example" assert checkio('bAse730onE4') == True, "2nd example" assert checkio('asasasasasasasaas') == False, "3rd example" assert checkio('QWERTYqwerty') == False, "4th example" assert checkio('123456123456') == False, "5th example" assert checkio('QwErTy911poqqqq') == True, "6th example"
|