2015-02-23 73 views
2

我試圖強行推銷代碼問題,並且我遇到了一些問題。試圖解決Python中的PIN算法

http://alvaray.org/brute-forcing-pincode-keypads-using-combinatorial-mathematics/

我包括鏈接討論我試圖解決這個確切的問題。現在它在頁面上包含了一個鏈接到公式的鏈接,但是我正在嘗試創建自己的鏈接而不使用它們。我的目標是將其從40,000個組合時期中縮短。如果我能達到10,003這將是很酷的,但是,最終目標是將其從最大值縮短。 我的代碼如下所示:

import random 

codes = [] 
shortest_combination = [] 
one_big_string = [] 
garbage_string = [] 
for a in range(0,10): 
    for b in range(0,10): 
     for c in range(0,10): 
      for d in range(0,10): 
       codes.append(str(a) + str(b) + str(c) + str(d)) 


random.shuffle(codes) 
shortest_combination.append(codes[0:1]) 
codes.remove(codes[0:1]) 
"".join(shortest_combination) 

for code in codes: 
    for short in shortest_combination: 
     codes.index(short[short + 1:len(short)]) 
     find = shortest_combination.append(codes[short: len(short) + 1]) 
     codes.remove(find) 
     "".join(shortest_combination) 

print shortest_combination 

不幸的是我得到這個錯誤: ValueError異常:list.remove(X):X不在列表中 我不知道我是我用錯了刪除?請幫助,加上關於如果我接近問題的任何反饋也是有幫助的。謝謝

回答

3

問題是codes[0:1]是一個元素的列表。你想要的是codes[0]

In [1]: codes = ["a", "b", "c"] 

In [2]: codes[0:1] 
Out[2]: ['a'] 

In [3]: codes[0] 
Out[3]: 'a' 
+0

哇哈哈哈我是白癡,謝謝你的工作。現在代碼的其餘部分是完全不同的困境。再次感謝 – Vantheman6 2015-02-24 20:27:11

0

原因你的錯誤已經由傑克·梅尼透露,所以我要回答你的另一個問題:

My goal is to shorten it from 40,000 combinations period. If i can get to 10,003 that would be cool

你想要什麼De Brujin sequence。維基百科頁面也鏈接在您讀過的關鍵字中,並且包含用於查找此類序列的代碼。它涉及構建一個De Brujin graph並在其中找到一個Eulerian cycle

不幸的是,如果不使用這種方法,將會很難提出一種(有效的)算法來減少長度。如果你確實管理好了,我很確定你的工作是可以發佈的。我建議你試着理解德布魯金圖如何構建背後的邏輯以及如何找到歐拉循環。

+0

不,是的,我看到了,但就像我說過的,我正在努力創造我自己的,我的朋友能夠做到,以及我的老闆用不同的公式。他把它縮短了幾個,這也是我的目標,而我的老闆通過他自己的扣除得到了10,004。但是,謝謝你的建議 – Vantheman6 2015-02-24 20:29:56