Problem set 4: the Caesar Cipher


pseudocode  for your solutions to Problems 2 and 4 by  Tuesday



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

pseudocode

 for your solutions to Problems 2 and 4 by 



Tuesday

. To do this, read problems 2 and 

4, and think about 

high level algorithms

 to solve both problems. Write down the steps in your 

algorithms and save it in a plain text file named ps4.txt. Upload this file to your workspace. 

On Wednesday, we will post our own pseudocode.

 You can use our pseudocode or your own 

(if it’s close enough), to write the Python code that actually solves problems 2 and 4. 

Problem 1. Encryption and Decryption 

Write a program to encrypt plaintext into ciphertext using the Caesar cipher. We have provided 

skeleton code for the following functions: 

def build_coder(shift): 

    """ 

    Returns a dict that can apply a Caesar cipher to a letter. 

    The cipher is defined by the shift value. Ignores non-letter characters 

    like punctuation and numbers. 

 

    shift: -27 < int < 27 



    returns: dict 

 

    Example:     >>> build_coder(3) 



    {' ': 'c', 'A': 'D', 'C': 'F', 'B': 'E', 'E': 'H', 'D': 'G', 'G': 'J', 

    'F': 'I', 'I': 'L', 'H': 'K', 'K': 'N', 'J': 'M', 'M': 'P', 'L': 'O', 

    'O': 'R', 'N': 'Q', 'Q': 'T', 'P': 'S', 'S': 'V', 'R': 'U', 'U': 'X', 

    'T': 'W', 'W': 'Z', 'V': 'Y', 'Y': 'A', 'X': ' ', 'Z': 'B', 'a': 'd', 

    'c': 'f', 'b': 'e', 'e': 'h', 'd': 'g', 'g': 'j', 'f': 'i', 'i': 'l', 

    'h': 'k', 'k': 'n', 'j': 'm', 'm': 'p', 'l': 'o', 'o': 'r', 'n': 'q', 

    'q': 't', 'p': 's', 's': 'v', 'r': 'u', 'u': 'x', 't': 'w', 'w': 'z', 

    'v': 'y', 'y': 'a', 'x': ' ', 'z': 'b'} 

    (The order of the key-value pairs may be different.) 

    """ 


    ### TODO. 

def build_encoder(shift): 

    """ 

    Returns a dict that can be used to encode a plain text. For example, you 

    could encrypt the plain text by calling the following commands 

    >>>encoder = build_encoder(shift) 

    >>>encrypted_text = apply_coder(plain_text, encoder) 

    The cipher is defined by the shift value. Ignores non-letter characters 

    like punctuation and numbers. 

 

    shift: 0 <= int < 27 



    returns: dict 

 

    Example:     >>> build_encoder(3) 



    {' ': 'c', 'A': 'D', 'C': 'F', 'B': 'E', 'E': 'H', 'D': 'G', 'G': 'J', 

    'F': 'I', 'I': 'L', 'H': 'K', 'K': 'N', 'J': 'M', 'M': 'P', 'L': 'O', 

    'O': 'R', 'N': 'Q', 'Q': 'T', 'P': 'S', 'S': 'V', 'R': 'U', 'U': 'X', 



    'T': 'W', 'W': 'Z', 'V': 'Y', 'Y': 'A', 'X': ' ', 'Z': 'B', 'a': 'd', 

    'c': 'f', 'b': 'e', 'e': 'h', 'd': 'g', 'g': 'j', 'f': 'i', 'i': 'l', 

    'h': 'k', 'k': 'n', 'j': 'm', 'm': 'p', 'l': 'o', 'o': 'r', 'n': 'q', 

    'q': 't', 'p': 's', 's': 'v', 'r': 'u', 'u': 'x', 't': 'w', 'w': 'z', 

    'v': 'y', 'y': 'a', 'x': ' ', 'z': 'b'} 

    (The order of the key-value pairs may be different.) 

    HINT : Use build_coder. 

    """ 


    ### TODO. 

def build_decoder(shift): 

    """ 

    Returns a dict that can be used to decode an encrypted text. For example, 

you 

    could decrypt an encrypted text by calling the following commands 



    >>>encoder = build_encoder(shift) 

    >>>encrypted_text = apply_coder(plain_text, encoder) 

    >>>decrypted_text = apply_coder(plain_text, decoder) 

    The cipher is defined by the shift value. Ignores non-letter characters 

    like punctuation and numbers. 

 

    shift: 0 <= int < 27 



    returns: dict 

 

    Example:     >>> build_decoder(3) 



    {' ': 'x', 'A': 'Y', 'C': ' ', 'B': 'Z', 'E': 'B', 'D': 'A', 'G': 'D', 

    'F': 'C', 'I': 'F', 'H': 'E', 'K': 'H', 'J': 'G', 'M': 'J', 'L': 'I', 

    'O': 'L', 'N': 'K', 'Q': 'N', 'P': 'M', 'S': 'P', 'R': 'O', 'U': 'R', 

    'T': 'Q', 'W': 'T', 'V': 'S', 'Y': 'V', 'X': 'U', 'Z': 'W', 'a': 'y', 

    'c': ' ', 'b': 'z', 'e': 'b', 'd': 'a', 'g': 'd', 'f': 'c', 'i': 'f', 

    'h': 'e', 'k': 'h', 'j': 'g', 'm': 'j', 'l': 'i', 'o': 'l', 'n': 'k', 

    'q': 'n', 'p': 'm', 's': 'p', 'r': 'o', 'u': 'r', 't': 'q', 'w': 't', 

    'v': 's', 'y': 'v', 'x': 'u', 'z': 'w'} 

    (The order of the key-value pairs may be different.) 

    HINT : Use build_coder. 

    """ 

    ### TODO. 

def apply_coder(text, coder): 

    """ 


    Applies the coder to the text. Returns the encoded text. 

 

    text: string 



    coder: dict with mappings of characters to shifted characters 

    returns: text after mapping coder chars to original text 

 

    Example: 



    >>> apply_coder("Hello, world!", build_encoder(3)) 

    'Khoor,czruog!' 

    >>> apply_coder("Khoor,czruog!", build_decoder(3)) 

    'Hello, world!' 

    """ 

    ### TODO. 

def apply_shift(text, shift): 

    """ 


    Given a text, returns a new text Caesar shifted by the given shift 

    offset. The empty space counts as the 27th letter of the alphabet

    so spaces should be replaced by a lowercase letter as appropriate. 

    Otherwise, lower case letters should remain lower case, upper case 




    letters should remain upper case, and all other punctuation should 

    stay as it is. 

 

    text: string to apply the shift to 



    shift: amount to shift the text 

    returns: text after being shifted by specified amount. 

 

    Example: 



    >>> apply_shift('This is a test.', 8) 

    'Apq hq hiham a.' 

    """ 

    ### TODO. 

Once you’ve written this function, you should be able to use it to encode strings. 


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ə