X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=cipherbreak.py;h=0d3fd7828d5d3fbd69f86453298975dd176a33e1;hb=317066e6551d143e38d093a55c4645dbd53c1c57;hp=95b5c208975c5035a2b2c2f50d5c3ebeb5f6bb8a;hpb=c66d9937e6ce523f9db7602997ad5e2924dfd7ec;p=cipher-training.git diff --git a/cipherbreak.py b/cipherbreak.py index 95b5c20..0d3fd78 100644 --- a/cipherbreak.py +++ b/cipherbreak.py @@ -131,14 +131,14 @@ def keyword_break(message, wordlist=keywords, fitness=Pletters): frequency analysis >>> keyword_break(keyword_encipher('this is a test message for the ' \ - 'keyword decipherment', 'elephant', 1), \ + 'keyword decipherment', 'elephant', Keyword_wrap_alphabet.from_last), \ wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS - (('elephant', 1), -52.834575011...) + (('elephant', ), -52.834575011...) """ best_keyword = '' best_wrap_alphabet = True best_fit = float("-inf") - for wrap_alphabet in range(3): + for wrap_alphabet in Keyword_wrap_alphabet: for keyword in wordlist: plaintext = keyword_decipher(message, keyword, wrap_alphabet) fit = fitness(plaintext) @@ -162,13 +162,14 @@ def keyword_break_mp(message, wordlist=keywords, fitness=Pletters, chunksize=500 frequency analysis >>> keyword_break_mp(keyword_encipher('this is a test message for the ' \ - 'keyword decipherment', 'elephant', 1), \ + 'keyword decipherment', 'elephant', Keyword_wrap_alphabet.from_last), \ wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS - (('elephant', 1), -52.834575011...) + (('elephant', ), -52.834575011...) """ with Pool() as pool: helper_args = [(message, word, wrap, fitness) - for word in wordlist for wrap in range(3)] + for word in wordlist + for wrap in Keyword_wrap_alphabet] # Gotcha: the helper function here needs to be defined at the top level # (limitation of Pool.starmap) breaks = pool.starmap(keyword_break_worker, helper_args, chunksize)