2 回答

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果有人要系統(tǒng)地嘗試所有不同的密碼,那么他需要迭代所有可能的組合,而不是嘗試兩次相同的組合。這是在 Python 中執(zhí)行此操作的一種方法:
import itertools
import string
real_pass = 'AAC'
def find_num_iterations_to_guess_password(pass_length):
all_letters = string.ascii_uppercase
iterations = 0
for i in itertools.product(all_letters, repeat=pass_length):
guess = ''.join(i)
if guess == real_pass:
print(f'the real password is {guess} and was guessed after {iterations}')
break
iterations += 1
find_num_iterations_to_guess_password(len(real_pass))

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
比隨機(jī)生成猜測(cè)并與已使用的選項(xiàng)列表進(jìn)行比較更好的選擇是通過(guò)算法創(chuàng)建所有可能的 n 長(zhǎng)度產(chǎn)品并迭代它們:
from itertools import product
import string
for i in product(string.ascii_uppercase, repeat=3):
if ''.join(i) == 'AAA':
print("Found")
break
編輯:使用product,不permutation
添加回答
舉報(bào)