辅导program编程设计、Python留学生程序讲解

- 首页 >> Python编程
Lab 2
Strings, Loops, and Ciphers
In lecture, we briefly discussed an example of using a cipher (where a message is put
into secret writing by encoding each letter to be represented by something else).
Step 1: Create your own cipher
Take out a pen and paper to brainstorm some ideas, your goal is to create your own
cipher that uses a one-to-one mapping of each letter of the alphabet to something else
(either a number, or another letter, etc.) in order to encode a message in a way that is
not easily decipherable.
However, you may create no more than three rules (so, create 1, 2 or 3 rules) for your
cipher. These rules are single statements that are used to transform the message.
Additionally, you must not change or add words or change word order and all
punctuation must be retained unaltered. Basically, all you are allowed to change are the
letters.
Here is an example of a cipher we could create:
Rule 1. The characters are divided into two groups: (1) characters that have an
enclosed area in uppercase and (2) characters that do not have an enclosed area in
uppercase.
Rule 2. Sort the two groups alphabetically, with group 1 first and then group 2.
Rule 3. Number the sorted list using 1-indexing and use the corresponding number for
each character as its code.
Here is the application of the cipher:
Applying Rule 1:
Group 1: {A, B, D, O, P, Q, R }
Group 2: {C, E, F, G, H, I, J, K, L, M, N, S, T, U, V, W, X, Y, Z}
Applying Rule 2:
A, B, D, O, P, Q, R, C, E, F, G, H, I, J, K, L, M, N, S, T, U, V, W, X, Y, Z
Applying Rule 3 will produce a 1-to-1 mapping table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
A B D O P Q R C E F G H I J K L M N S T U V W X Y Z
This will translate the message:
"We drove to the gym and swam in the pool."
To:
23_9 3_7_4_22_9 20_4 20_12_9 11_25_17 1_18_3 19_23_1_17 13_18
20_12_9 5_4_4_16
If you were to try to make each character encode to another randomly selected
character, it would require 26 rules, and that would violate the above requirements. That
is, you must find a way to treat each character to a transformation using the same set of
rules. Any special characters, such as whitespace or punctuation, should be left as is.
Although for readability's sake like in the cipher above, we had to indicate character
breaks with a _ symbol, and tripled the space between each word.
Step 2: Write an encoded message
Take a piece of paper (or anything you can scribble/type on) and encode a few
messages using your method. You don’t need to hand this in, but it will help you in the
next step as you will have something to refer to when implementing your cipher.
Step 3: Write an encoding program
Open up a new file called cipher.py and write a function that takes in an English
sentence and encodes it according to your personal encoding algorithm. The function
should have a type contract str -> str, that is it takes in a string and returns an encoded
string.
This file should have the rules you came up with in step 1 as comments at the top of the
file or as a multi-line string.
Submit cipher.py to MarkUs once you are done.
Step 4 (Optional): Write a decoding program
Optionally, write another function that decodes an encoded string. That is, given an
encoded string output from the function you wrote for step 3, this function should
decipher it and return the original string before it was encoded.
If you decide to do this step, simply submit it along with the code in step 3, the TAs will
see that you have an additional function when marking.

站长地图