Lab 2, CSC430, Winter 2026
1 Discovering errors
2 Cracking Encryptors
2.1 Instructions
9.0.900

Lab 2, CSC430, Winter 2026🔗

back to course homepage

1 Discovering errors🔗

Each of the following lines of code will result in an error. One by one, run them in Pyret, discuss the error, and fix the code so that it runs (you can make assumptions about functionality or order of operations if necessary). For some of the examples, you or your partner might see the issue with the code before even running it – talk about the error message anyway. One of the points of this task is to start getting used to individual error messages so that it’s less scary to encounter them when working on more complex code later on. If you get stuck, browse the Pyret documentation on the Number, String, and Image functions to see if that helps guide you in the right direction before asking for help.

  • Draw a small hexagon: regular-polygon("orange", "solid", 6, 100)

  • Compute a complex math expression: num-sqrt(3*3+4*4)

  • Get the last two characters of a word: string-substring("bees", 3, 5)

  • Get the last letter of a word: string-char-at("Frankenstein", string-length("Frankenstein"))

  • Get the absolute difference of the length of two words: num-abs(string-length("Frankenstein", "monster"))

2 Cracking Encryptors🔗

You’re on a top secret mission and need to communicate an encrypted message containing important code. However, the instructions to your encryption machine were accidentally thrown into a hay bale, and now it’s up to you to figure out how the machine works!

The encryptor machine has 10 black-box functions that encrypt a String. Your first task is to test various inputs and figure out how each function changes the input based on the output.

Once you have an idea of what an encryption function does, write a version of the function that encrypts a String in the same manner and run it through the checks in the support file. If all checks pass, then you’ve successfully built an encryptor for covert communications!

Hint: The encryptors are composed of functions from the Pyret Strings library. Start by reading through it.

Here is an example of what an encryptor looks like under the hood:

provide *
provide-types *
 
fun encryptor0(s :: String) -> String:
  doc: "returns the first letter of the string"
  string-substring(s, 0, 1)
end
 
check "encryptor0: empty str":
  encryptor0("") raises "index"
end
 
check "encryptor0: general":
  encryptor0("a") is "a"
  encryptor0(" ") is " "
  encryptor0("hello") is "h"
  encryptor0("1, ") is "1"
end
 
 

We created an encryptor called encryptor0 that takes in a string and returns the first letter only. We uqse check blocks to ensure that it works correctly given empty and non-empty strings.

2.1 Instructions🔗

Include this at the top of your program (before any other code, including code from the previous parts):

 import shared-gdrive("lab2-support-2026.arr", "1r1cGY22vvBTx1zl3XnByfViVK0OmK_d0") as support

Now you will be able to access all of the encryptors and their testers in your Pyret file.

The encryptor functions are named

support.encryptor1,
support.encryptor2,
support.encryptor3,
support.encryptor4

The encryptor functions take in Strings as input (e.g: support.encryptor1("mystery")) and produce an encrypted result.

** Note: Please keep a record of what Strings you use to test each encryptor – you should be prepared qto explain your choice of Strings to a TA. They will be useful for you to reference, too! **

The tester functions are named:

support.test-encryptor1,
support.test-encryptor2,
support.test-encryptor3,
support.test-encryptor4

To use them, you must use the name of your function as an input. For example, after giving some inputs to support.encryptor1, you may think it always returns the String "cs0111" (HINT: it doesn’t do this – just an example). Then you would write the function:

fun my-cool-encryptor(str :: String) -> String:
    "cs0111"
end

You would test whether your function is correct (meaning it does the same thing as encryptor1 for all inputs) by running:

support.test-encryptor1(my-cool-encryptor)

Hint: The Pyret Strings library will be very helpful when writing your functions!