Lab 2 — String Encryptors
Date: September 22, 2026
Purpose
Learn how to define functions, and about string library
Skills: 0, 1
Introduction
You’ve stumbled upon a malfunctioning encryption device that scrambles secret messages into code. Unfortunately, the documentation went missing! Your mission is to reverse-engineer each of the device’s ten black-box functions so that you can replicate their behavior in Python.
How it works
The device exposes ten functions, named support.encryptor1 through support.encryptor10.
Each encryptor takes in a String and returns a new, “encrypted” String.
Alongside each encryptor, there’s a corresponding tester routine — support.test_encryptor1, support.test_encryptor2, and so on that — checks whether your replication matches the original for all possible inputs.
Instructions
-
Experiment
- Call each
support.encryptorNwith a variety of sample strings. - Observe how the output changes and identify the pattern or rule it applies.
- Keep a written log of the inputs you tried and the outputs you observed (remember, you are acting as a detective here!).
- Call each
-
Recreate
- For each encryptor, write your own function, for example:
def my_encryptor1(s: str) -> str:
...which implements the same transformation you discovered.
- Use Python’s string operations (e.g.
len, slicing withs[start:end],s.find(...), etc.) to build your own version.
-
Verify
- Pass your function to the provided tester:
support.test_encryptor1(my_encryptor1)
If the tester completes without errors, your encryption logic is correct!
The goal of the lab is to recreate as many of the encryptors as you can!
Here’s how we built an encryptor that returns only the first character of any string (s[0] is the character at position 0 of s; to test that code causes an error, we use with pytest.raises(...), which checks that the code indented under it causes an error of the given kind -- here, an IndexError, since an empty string has no character at position 0):
import pytest
def encryptor0(s: str) -> str:
"""returns the first letter of the string"""
return s[0]
def test_encryptor0():
# Doesn't work on empty strings
with pytest.raises(IndexError):
encryptor0("")
# Otherwise, returns first letter
assert encryptor0("a") == "a"
assert encryptor0(" ") == " "
assert encryptor0("hello") == "h"
assert encryptor0("1, ") == "1"
More Hints!
- When choosing Strings to use as inputs to the encryptors, it will be useful to vary your inputs – think about why this could be. In what ways can you vary your Strings?
- Think about scenarios where we might see strings of text, such as a book or report. What symbols might you commonly see in that text, other than alphabetical letters?
- Remember that strings in Python are 0-indexed. This means that the first character in a string will be assigned index 0 (not 1).
- Encryptor 7 is one of the trickiest. Try strings of various lengths that start with the same letters. What do you notice?