Problem set 4: the Caesar Cipher



Yüklə 111,01 Kb.
Pdf görüntüsü
səhifə5/10
tarix17.02.2022
ölçüsü111,01 Kb.
#83821
1   2   3   4   5   6   7   8   9   10
MIT6 00SCS11 ps4

Part b: Python code 

Implement find_best_shift. This function takes a wordlist and a bit of encrypted text and 

attempts to find the shift that encoded the text. A simple indication of whether or not the correct 

shift has been found is if all the words obtained after a shift are valid words. Note that this only 

means that all the words obtained are actual words. It is possible to have a message that can be 

decoded by two separate shifts into different sets words. While there are various strategies for 

deciding between ambiguous decryptions, for this problem we are only looking for a simple 

solution. 

To assist you in solving this problem, we have provided a helper function: 

is_word(wordlist, word)

. This simply determines if word is a valid word according to wordlist. 

This function ignores capitalization and punctuation. 

Hint: You may find the function string.split to be useful for dividing the text up into words. 

def find_best_shift(wordlist, text): 



    """ 

    Decrypts the encoded text and returns the plaintext. 

 

    text: string 



    returns: 0 <= int 27      

 

    Example:     >>> s = apply_coder('Hello, world!', build_encoder(8)) 



    >>> s 

    'Pmttw,hdwztl!' 

    >>> find_best_shift(wordlist, s) returns 

    8 


    >>> apply_coder(s, build_decoder(8)) returns 

    'Hello, world!' 

    """ 

    ### TODO 

Once you’ve written this function you can decode your friend’s emails! 


Yüklə 111,01 Kb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   10




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə