題目描述
密碼要求:
1.長度超過8位
2.包括大小寫字母.數字.其它符號,以上四種至少三種
3.不能有相同長度超2的子串重復
說明:長度超過2的子串
輸入描述:
一組或多組長度超過2的子符串。每組占一行
輸出描述:
如果符合要求輸出:OK,否則輸出NG
示例1
輸入
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
輸出
OK NG NG OK
Python code:
def fun1(str): if len(str) > 8: return 1 else: return 0 def fun2(str): num1 = 0 num2 = 0 num3 = 0 num4 = 0 for i in str: if 'a'<=i and i<='z': num1 = 1 elif 'A' <=i and i<='Z': num2 = 1 elif '0' <=i and i<='9': num3 = 1 else: num4 = 1 if (num1+num2+num3+num4) >=3: return 1 else: return 0 def fun3(str): for i in range((len(str)-3)): if str[i:i+3] in str[i+1:]: return 0 break return 1 while True: try: str1 = input() if fun1(str1) and fun2(str1) and fun3(str1): print('OK') else: print('NG') except: break
思路:將每個條件單獨考慮,寫成一個個函數。不定多少組輸入時,用while循環。