程序代写案例-COMP16321
时间:2021-10-14

COMP16321 - Programming 1 Week 02 Laboratory Iteration Introduction In the first week you were introduced to state with examples in the lecture, i.e. the lightbulb and traffic lights. Last week’s lecture covered iteration, one of the more difficult programming constructs to grasp. To reinforce your understanding of iteration this lab will look at one of the earlier and most widely known encryption tech- niques known as the Caesar Cipher (aka Shift Cipher and Caesar Shift). Each letter in the plain text is substituted by a letter from the same alphabet, the new letter chosen depends on the shift number. The table below shows the results of a shift of three. A B C D E F G H I J K L M O P Q R S T U V W X Y Z X Y Z A B C D E F G H I J K L M O P Q R S T U V W If the letter in the plaintext was H we shift left by three (G, F, and E ) to obtain the character for the ciphertext. The Caesar Cipher is named after Julius Caesar who used it with a shift of three to protect messages of military significance. Although the Caesar cipher is not ‘secure’ for today’s applications it does form the basis of more complex encryption algorithms Topics Covered in this Lab 1. Interpreting pseudocode. 2. Tracing through an algorithmic design 3. Converting pseudocode to Python. 4. Obtaining input from the user. 5. Variable (declaration, initialisation or as- signment) 6. While loops 7. Basic functions (len) 8. The array operator and lists 9. Negating an evaluation 10. The ASCII table 11. Less common functions (ord and chr) Page 1 of 9 Git, GitLab and Cloning your Existing Repository You should have cloned the repository COMP16321-Labs for Lab 1. If you haven’t

done so, clone this repository. In the repository create a new branch, name it something sensi-
ble such as Lab2-Iteration (do not call it the same name as the tag name). This is the branch
that you will use for this lab.
Complete your lab exercises saving the exercises as appropriate (in the repository), and adding,
committing, and pushing to the remote repository at regular intervals.
Once you have completed the lab exercises, add, commit, and push as normal. Then create a
tag named Lab-Iteration, push to GitLab and then push to GitLab with tags.
Check SPOT to ensure submission was successful.
Part 1: Trace Tables
Below is an initial design of an encryption algorithm in pseudocode. Using the trace table,
trace through the algorithm for the plaintext: HELLO. The first five instructions have been
done to get you started. You are to reproduce the trace table in a suitable application such as
excel or use pen and paper. You are not submitting your trace table, but keep note of the final
value in cipherText because you can test if you correctly traced through the algorithm when
you code it.
Algorithm 1 Encryption Algorithm
1: plaintext ← read(‘Plaintext:’)
2: cipherTest ← “ ”
3: alphabet ← “XYZABCDEFGHIJKLMNOPQRSTUVWXYZABC”
4: plaintextPosition ← 0
5: while (plaintextPosition < length(plaintext)) do
6: plaintextChar ← plaintext[plaintextPosition]
7: alphabetPosition ← 3
8: while plaintextChar 6= alphabet[alphabetPosition] do
9: alphabetPosition ← alphabetPosition + 1
10: end while
11: alphabetPosition ← alphabetPosition - 3
12: cipherText ← cipherText + alphabet[alphabetPosition]
13: plaintextPosition ← plaintextPosition + 1
14: end while
15: display(cipherText)
Page 2 of 9
Line Number Action
1 plaintext variable becomes equal to “Hello”
2 cipherText variable becomes equal to “ ”
3 alphabet variable becomes equal to “XYZABC...XYZ”
4 plaintextPosition variable becomes equal to 0
5 0 is less than 5 so condition evaluates to true
Part 2: Encryption Program
Your next tast is to convert the design to Python code and run the program. Use the file
encryption.py you created earlier.
1. Have a go at converting the pseudocode to Python yourself first. If you get stuck, you
can refer to the sub steps (1-13 ) below.
Line 1: Ask the user to input some plaintext. Store the user unut in a variable called
plaintext.
Line 2: Declare a variable called cipherText to store the resulting ciphertext. We
initialise this to an empty string because we have not yet started the conversion
process.
Line 3: Declare a variable to store the alphabet, assign the alphabet to this variable.
Note: When you begin testing your final solution, try to understand why the
alphabet started XYZ and finished ABC.
Line 4: We convert the plaintext to ciphertext character by character, so we need to
keep track of which character we are processing in the plaintext. In Python
strings start at 0 so we assign the value 0 to the plaintextPosition variable
so that it begins at the beginning of the string.
Line 5: We want to process each character in the plaintext until we have reached the
end of the plaintext.
Note: Do you understand how the len function works?
Line 6: We copy the character from the plaintext into a variable called plaintextChar.
Note: This instruction is part of the while loop block so should be indented ac-
cordingly.
Line 7: We aim to find a matching character from the alphabet, so we start the search
at the first occurrence of A in the alphabet.
Note: This instruction is part of the while loop block so should be indented ac-
cordingly.
Line 8: While we have not found the matching character in the alphabet we keep iter-
ating through the alphabet.
Page 3 of 9
Note: ! in this context is synonymous to not. Thus, while plaintextChar is not
equal to alphabet[alphabetPosition].
Note: This while statement is part of the previous while statement. Thus, we
have created an inner-while block.
Line 9: We want to move through the alphabet character by character, so we increment
our alphabetPosition variable so that it moves to the next character.
Note: This instruction is part of the inner-while loop block so should be indented
accordingly.
Line 10: When a match is found the will loop evaluates to false and finishes. Then we
decrement the alphabetPosition variable by three so that we are pointing three
characters to the left of the matching character in the alphabet.
Note: This instruction is part of the while loop block (not the inner-while loop
block) so should be indented accordingly.
Line 11: We concatenate the letter from the alphabet at the position determined by
alphabetPostion to what is already in the cipherText variable.
Note: This instruction is part of the while loop block (not the inner-while loop
block) so should be indented accordingly.
Line 12: Next we need to process the next letter of the plaintext so we increment the
plaintextPostion variable by one.
Line 13: Once all characters in the plaintext have been processed, we display the re-
sulting cipherText.
Note: This instruction is not part of the while block, nor the inner-while block.
It should not be indented.
2. Let’s test the program. Our alphabet is restricted to uppercase characters A-Z. So, no
other characters are allowed. Try to encrypt the following plaintext messages.
(a) LOVEPYTHON
(b) LOVEUOM
(c) FULLOFLOVE
3. If your program works as expected you should add, commit and push to the remote repos-
itory.
Expected Results Are:
(a) ILSBMVQELK
(b) ILSBRLJ
(c) CRIILCILSB
Page 4 of 9
Part 3: Decryption Program
1. Re-save the program as decryption.py
2. The program in part 2 allows us to create a ciphertext from plaintext. Can you amend
the program so that it converts a ciphertext to a plaintext? Use the program to decode
the following ciphertexts.
(a) EBIIL
(b) DOBXQGLYPLCXO
(c) KBUQTBTFIIXAXMQLROXIMEXYBQ
Notes:
i) If we shift left by three to create the cipher text, what do you think we need to
do to reverse the process?
ii) You should also change the input message since you are inputting ciphertext
not plaintext.
3. If your program works as expected you should add, commit and push to the remote
repository. You will know if the program works if the decryption yields in messages that
you understand.
Part 4: A Better Design
Our alphabet only consisted of uppercase characters A-Z, so any other input would cause the
program to fail. We could extend our alphabet by adding characters a-z, numeric digits, a
space, etc.
A better way would be to use an existing alphabet that already contains all characters including
special characters like * and |. We could use the ASCII table to achieve this. See Figure 1.
The table gives the decimal, hexadecimal and octal representation for a given character.
1. Using an existing alphabet will simplify and enhance our program, let’s examine the
pseudocode (Algorithm 2) and complete the tracetable for the plaintext ‘Hello’. Add new
lines to the trace table as necessary.
Note: GetASCIIValue(plaintextChar) will return the decimal value for the
character stored in plaintextChar. GetCharValue(ASCIIValue) will return
the character a given decimal value.
Page 5 of 9
Figure 1: The ASCII Table
Step Number Action
1 plaintext variable becomes equal to “Hello”
2 cipherText variable becomes equal to “ ”
3 plaintextPosition variable becomes equal to 0
4 0 is less than 5 so condition evaluates to true
Algorithm 2 ASCII Algorithm
1: plaintext ← read(‘Plaintext:’)
2: cipherTest ← “ ”
3: plaintextPosition ← 0
4: while (plaintextPosition < length(plaintext)) do
5: plaintextChar ← plaintext[plaintextPosition]
6: ASCIIValue ← GetASCIIValue(plaintextChar)
7: ASCIIValue ← ASCIIValue - 3
8: cipherText ← cipherText + GetCharValue(ASCIIValue)
9: plaintextPosition ← plaintextPosition + 1
10: end while
11: display(cipherText)
Page 6 of 9
Part 5: Encryption Program Version 2
Your next task is to convert the design to Python code and run the program. Create a new file
called encryption2.py.
1. Create a new program by converting the pseudocode in part 4. Those not too familiar
with Python might find the following information useful.
Notes:
i) ASCIIValue = ord ( p l a i n t e x t C h a r )
The ord() function in Python, given a string of length one will return an
integer representing the Unicode code point of the character. For example,
ord('a') returns the integer 97, ord('€') returns 8364.
ii) CharValue = chr ( ASCIIValue )
The chr() function in Python, given an integer value will return the repre-
senting character at that Unicode point. For example, chr(97) will return the
character ‘a’ and chr(8363) will return the character ’‘€’.
2. Let’s test the program. Our alphabet is no longer restricted so we can try to convert any
string of characters. Try to encrypt the following plaintext messages.
(a) COMP16321 is great.
(b) Gareth’s workshop (take off) puzzles are tricky but fun.
(c) The meaning of life is 42.
3. If your program works as expected you should add, commit and push to the remote
repository.
Expected Results Are:
(a) @LJM.30/.fpdob^q+
(b) D^obqe‖ptlohpelm\%q^hblcc&mrwwibp^obqof`hv_rqcrk+
(c) Qebjb^kfkdlcifcbfp1/+
Note: LATEXformats things in a strange way, if the results aren’t exactly the same
don’t worry.
Page 7 of 9
Part 6: Decryption Program Version 2
1. Resave the program as decryption2.py
2. The program in part 5 allows us to create a ciphertext from plaintext. Can you amend
the program so that it converts a ciphertext to a plaintext? Use the program to decode
the following ciphertexts.
(a) @lafkdfpCrk
(b) F\^j\pl\dlla\^q\pibbmfkd+\F\`^k\al\fq\tfqe\jv\bvbp\`ilpba+
(c) >tbpljb\gl_\tbii\alkb+
Notes:
i) If we shift left by three to create the cipher text, what do you think we need to
do to reverse the process?
ii) You should also change the input message since you are inputting ciphertext
not plaintext.
3. If your program works as expected you should add, commit and push to the remote
repository. You will know if the program works if the decryption yields messages that you
understand.
4. Now that you get the general idea be creative and have some fun. Make the program your
own by adding complexity. You may want to consider:
- Different shift values
- Varying shift values
- Asking the user if they want to encrypt or decrypt
- Multiple rounds of encryption/decryption
- Be creative!
Page 8 of 9
Conclusion
In this laboratory we looked at interpreting pseudocode, tracing through an algorithmic design,
converting pseudocode to Python, obtaining input from the user, variable (declaration, initial-
isation or assignment), while loops, basic functions (len), the array operator and lists, negating
an evaluation, the ASCII table, and some less common functions (ord and chr)
If you haven’t already done so, push the programs that you created to the remote repository.
End of Laboratory Exercises
Page 9 of 9

学霸联盟


essay、essay代写