CS414 Lab 9: Cypher Tools

For this lab, you will prepare a several files, one for each exercise.

Before you begin, create a folder for your programs. Then download these starting files:

This file is available off the lab's page:
cs.unh.edu/~cs414/labs/lab09/cs414-lab9.html

Turning in your work

Be sure to turn in whatever you have finished, at the end of the lab session. You may continue to work until midnight, and submit your work again.

Late penalties: 1 day late: -5%, 2 days: -10%, -3 days: -20%.

To turn your work in, go to mycourses.unh.edu, find CS414, open the syllabus, and click on Lab 8. Then click Submit Assignment, click Choose file, and upload crypto.py .

Exercises:

  1. String replacement : Run IDLE, and open the file crypto.py. The function replace_text() doesn't work: run the program to see for yourself.

    The error is in this line:

                text[index] = new_text
              
    The problem is that strings are immutable (you can't change any part of a string). Try using list slices instead:
                text[index] = text[ : index] + new_text + text[index + 1 :]
              
    Also, try another approach: explode the letters into a list, change a list entry, and join the letters again:
                text_letters = list(text)
                text_letters[index] = new_text
                text = ''.join(text_letters)
              

  2. Scramble a string : Implement the function scrambled() .

    Proceed as follows:

  3. Caesar cypher : Implement the function shift_encypher() .

    Notice the string alphabet. Initialize it as follows:

                alphabet = string.lowercase
              
    (This has the same result). You will now create a cypher alphabet, with the letters shifted forward by shift spaces. For example, if shift==3, you should get 'defghijklmnopqrstuvwxyzabc'.

    To do this, you should initialize cypher_alphabet by joining together two slices of alphabet:

    The remaining job is to replace each letter in text with the corresponding letter in cypher_alphabet. Proceed as follows:

  4. Caesar cypher 2 : Implement shift_decypher, which performs the action opposite to shift_encypher. This function is almost identical, except that you should find a location in cypher_alphabet, and get the same letter in alphabet

  5. Reflowed text : Implement print_reflowed, which opens a file, reads the text, and prints its text with a maximum witdh.

    The function includes code to read a file and join all its text into one long string. The remaining task is this:

    Don't forget to reset the line length at the right time.