<1>自動售貨機系統代碼
自動售貨機運行結果
如何快速生成驗證碼,快速生成內推碼
import random
import string
code_str = string.ascii_letters + string.digits
# 方法一:
def gen_code(len=4):
code = ''
for i in range(len):
code += random.choice(code_str)
print(code)
gen_code()
# 方法二:
def gen_code(len=4):
return ''.join(random.sample(code_str,len))
print([gen_code(len=6) for i in range(5)])
print([gen_code(len=8) for i in range(5)])
3. 快速生成驗證碼 / 內推碼
題目描述:
密碼要求:
1.長度超過8位
2.包括大小寫字母.數字.其它符號,以上四種至少三種
3.不能有相同長度超過2的子串重複
說明:長度超過2的子串
輸入描述:
一組或多組長度超過2的子符串。每組佔一行
輸出描述:
如果符合要求輸出:OK,否則輸出NG
示例1:
輸入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
輸出:
OK
NG
NG
OK
分析:
判斷是否有長度超過2的子串重複,只需拿出長度爲3的任意子串與剩下的比較
# 判斷密碼長度
def passwd_len(str):
if len(str) > 8:
return True
else:
return False
# 判斷密碼組成
def passwd_consist(str):
digit,upper,lower,other = 0,0,0,0
for i in str:
if i.isdigit():
digit = 1
elif i.isupper():
upper = 1
elif i.islower():
lower = 1
else:
other = 1
sum = digit + upper + lower + other
if sum >= 3:
return True
else:
return False
# 判斷密碼重複
def passwd_rep(str):
# 一次拿出一個長度爲3的子串,則只需循環len(str)-3次即可遍歷完字符串
for i in range(len(str)-3):
# 如果相同子串的數量大於1,即子串重複,返回False
if str.count(str[i:i+3]) > 1:
return False
return True
while True:
s = input('請輸入密碼:')
if s == 'q':
break
if passwd_len(s) and passwd_consist(s) and passwd_rep(s):
print('OK')
else:
print('NG')