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
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
.
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_textThe 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)
Scramble a string :
Implement the function scrambled()
.
Proceed as follows:
text
into a list
random.shuffle()
, which modifies
the list
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
:
shift
characters
shift
characters.
text
with the corresponding letter
in cypher_alphabet
. Proceed as follows:
""
.
text
:
for letter in text: ...
letter
is in the
alphabet (the text
may have spaces or
punctuation). If not, just add the original letter
to the result.
letter
within alphabet
(use
the index()
function).
cypher_alphabet
and add it to the
result.
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
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:
width
, do a print
first.
,
to continue the current line.