CS-UY 1114: Lab 7
Midterm Review
You must get checked out by your lab CA prior to leaving early. If you leave without
being checked out, you will receive 0 credits for the lab.
Restrictions
The Python structures that you use in this lab should be restricted to those you have learned
in lecture so far (topics learned in Rephactor may only be used if they have also been taught
in lecture). Please check with your teaching assistants in case you are unsure whether
something is or is not allowed!
Create a new Python file for each of the following problems.
Your files should be named lab[num]_q[num].py, similar to homework naming
conventions.
Submit your work on Gradescope under Lab 7. The deadline is October 19th, 11:59 PM.
1
Problem 1: String Fun!
Solve this problem by hand.
Evaluate each snippet of code and show what prints to the screen for each print statement.
You can treat the entire code segment as a single, continuous program. Write “ERROR” if an
error would occur. If nothing is printed, then write “NO OUTPUT”.
statement1 has one leading and one trailing space. Indices of each character are also
provided for your convenience.
#
1
2
3
# INDEX
012345678901234567890123456789012345
statement1 = “ The solution is to find solutions! “
1. Write the output of the following code snippet:
length = len(statement1)
position1 = statement1.find(“ “, 17)
print(statement1[position1 + 1:length])
2. Write the output of the following code snippet:
trim_stmt = statement1.strip()
position2 = trim_stmt.find(“io”, 31)
print(trim_stmt[:23:position2])
3. Write the output of the following code snippet:
start = trim_stmt.find(“Solution”)
end = trim_stmt.find(“!”, start)
print(statement1[start:end])
2
statement2 is defined as follows:
#
1
2
# INDEX
012345678901234567890123
statement2 = “Computer Science is fun!“
4. Write the output of the following code snippet:
pos_is = statement2.find(" is ")
sub_str1 = statement2[:pos_is:-1]
print(sub_str1.upper())
5. Write the output of the following code snippet:
count_e = statement2.count("e")
sub_str2 = statement2[::count_e]
statement2 = statement2.replace(“ “, “#”)
print(sub_str2.find(“e”))
6. Write the output of the following code snippet:
count_i = statement2.count("i")
sub_str3 = statement2[::count_i]
space_pos = statement2.index(“ “)
print(sub_str3)
3
Problem 2: Modern Caesar
You work as a secret agent and need to deliver messages to your team. However, in the
modern world, you don’t deliver messages in person anymore. You use your own custom
Caesar Shell! You can send short messages and decipher your teammates’ messages as well.
Before you start using the shell, you have to get trained and become familiar with commands.
Here are the supported commands:
● ENC:<k>:<message> - encrypt <message> by shifting letters forward by integer k
● DEC:<k>:<message> - decrypt <message> by shifting letters backward by integer k
● SHIFT: <k> - set a default shift key
● HELP - print one line describing each command
● QUIT - exit the shell
If you use SHIFT: <k> , your default key will be used in ENC::<message> and
DEC::<message>. (Note the double colon). If you use double colons without setting your
default key first, ERROR will be printed.
Caesar Cipher Rules
A Caesar cipher is a simple substitution cipher where each letter in a message is shifted a
fixed number of positions down the alphabet, with letters at the end wrapping around to the
beginning. For example, with a shift of 3, ‘A’ becomes ‘D’, ’B’ becomes ‘E’, and ‘a’ becomes ‘d’.
To encrypt, shift the letters forward. To decrypt, shift them backward by the same amount. If
you approach the end of the alphabet, you can wrap around. For example, ‘z’ with a shift of 2
becomes ‘b’.
You can assume that <message> will always include only letters (no spaces or punctuation).
You can also assume that parts of the command are separated by colons and that all
commands are written in uppercase. For printing errors, take a look at the sample outputs and
figure out what you need to check for before encrypting/decrypting/printing a message/etc.
If you’re unsure about how to approach a problem or what assumptions you can make, ask
your CAs.
4
Here is the sample output of the program:
Enter the command: HELP
ENC:<k>:<message> encrypt letters by shift k
DEC:<k>:<message> decrypt letters by shift k
SHIFT:<k>
set default shift key
HELP
print this help line
QUIT
exit the shell
Enter the command: ENC:3:Hello
Khoor
Enter the command: DEC:3:Khoor
Hello
Enter the command: SHIFT:5
Enter the command: ENC::ABC
FGH
Enter the command: DEC:2:ABC
YZA
Enter the command: DEC:2:abc
yza
Enter the command: QUIT
Here is another sample output:
Enter the command: ENC::ABC
ERROR - double colon but no default key is set yet
Enter the command: ENC:x:ABC
ERROR - key can only be a positive integer
Enter the command: WRONG:2:ABC
ERROR - unrecognized command “WRONG”
Enter the command: QUIT
5