# Lab 1: Message Decryptor ## Part 1 - Get to know each other! Welcome to CSCI 0111 Lab! We're *so* excited to work with you. Now that you're with your partner(s), discuss one/some/all of these questions: * What is your favorite food? * Do you know the difference between bees and wasps? * What do you do for fun? --- ### CHECKPOINT: Call a TA over to discuss the questions above! --- ## Part 2 - Practice reading error messages When writing code, it is common to encounter error messages. This doesn't only happen when learning: because programming languages are very particular in what they allow, it's common for professional programmers to encounter and deal with errors while working on a piece of code. Just like anything else we learn in CS111, learning how to read an error message is a skill we can practice. Let's do that now. For example, let's say we want to figure out the number of digits that `111` has. If we remember that `string-length` tells us how many characters the input has, we might run the expression `string-length(111)` and see what happens: ![strlenerr](https://hackmd.io/_uploads/ByUnw9mhA.png) This error message tells us that the first (and only) input to `string-length` should be a `String`, which `111` does not satisfy (it's ok if this is not immediately obvious to you from the error message. As you work more with Pyret, you'll pick up on some of the patterns in the wording of the error message.) Take a few moments to chat with your neighbor and identify how to fix this error before reading the answer below. :::spoiler The fix Because `string-length` expects a String input, we can put quotes around `111` so that Pyret interprets the value as a `String.` Try running `string-length("111")` to see that the error is fixed. ::: 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. 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](https://pyret.org/docs/latest/Builtins_and_Libraries.html) on the Number, String, and Image functions to see if that helps guide you in the right direction before asking for help. 1. Draw a small hexagon: `regular-polygon("orange", "solid", 6, 100)` 2. Compute a complex math expression: `num-sqrt(3*3+4*4)` 3. Get the last two characters of a word: `string-substring("bees", 3, 5)` 4. Get the last letter of a word: `string-char-at("Frankenstein", string-length("Frankenstein"))` 5. Get the absolute difference of the length of two words: `num-abs(string-length("Frankenstein", "monster"))` ___ ### CHECKPOINT: Call over a TA after you have finished problems 1-5. ___ ## Part 3 - Edge Cases Code can often be brittle in the face of inputs unforeseen by the programmer (edge cases). 1. Read this [short article](https://www.theverge.com/tldr/2019/8/14/20805543/null-license-plate-california-parking-tickets-violations-void-programming-bug) from the Verge detailing how a novelty license plate exposed a huge flaw in the ticketing system. Algorithms that include these limitations often disproportionately impact marginalized and/or vulnerable individuals and/or groups. Read this [article](https://www.nilc.org/news/the-torch/5-11-17/) about how the REAL ID Act’s identification requirements create unique obstacles for women, people of color, and low-wage workers. 2. Discuss the importance of edge cases in real life and how you can use them to test a function with unknown behavior. ___ ### CHECKPOINT: Call over a TA and share your takeaways from your discussion. ___ <!-- ## Problem 2 - Data in the Real World: Google Ads ![this-ad-thinks-it-knows-you](https://i.imgur.com/0HCbaPc.png) We begin this lab by demonstrating how data plays a role in your life, whether you are aware of it or not. For example, information that a search engine has on you comes from a combination of your profile and predictions that the search engine makes about you. These predictions are based on your browsing history, users that are similar to you, etc. Look at some of the information that Google gathers and infers about users. You can check your own data by visiting https://myadcenter.google.com/ or look at the picture below for an example. ![ad-personalization information](https://i.imgur.com/h8E3t3U.png) Using either your data or the picture above identify pieces of information that are inferred about you by Google With your partner, **discuss how Google may have come to infer those assertions about you and how this information might be used when making ad targeting decisions**. ___ ### CHECKPOINT: **Call over a TA to discuss an example of something you found surprising when looking over the data.** ___ --> ## Part 4 -- 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 the pond, and it’s up to you to figure out how it 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](https://www.pyret.org/docs/latest/strings.html). 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 use ```check``` blocks to ensure that it works correctly given empty and non-empty strings. ### Instructions Include this at the **top** of your program (before any other code, including code from the previous parts): ```import shared-gdrive("lab1-support-2022.arr", "1kS_-rd0eE9-DLndIYw-0Bez8c6J73MRK") 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, ... support.encryptor10 ``` The encryptor functions take in Strings as input (e.g: ```support.encryptor1("mystery")```) and produce an encrypted result. :::info Please keep a record of what Strings you use to test each encryptor – you should be prepared to explain your choice of Strings to a TA. They will be useful for you to reference, too! ::: The *tester functions* are named, inspiringly: ``` support.test-encryptor1, support.test-encryptor2, support.test-encryptor3, support.test-encryptor4, ... support.test-encryptor10 ``` 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](https://www.pyret.org/docs/latest/strings.html) will be very helpful when writing your functions! :::spoiler 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? ::: ___ ### CHECKPOINT: 1. Call over a TA after you have finished encryptors 1-4. 2. Call over a TA after you have finished encryptors 5-8. ___ Now it's time to prove your encryptor expertise - let's solve the final two encryptors in the `support` code, `support.encryptor9` and `support.encryptor10`. **Hint:** Encryptor 9 is one of the trickiest. Try strings of various lengths that start with the same letters. What do you notice? --- ### CHECKPOINT: Call over a TA once you have finished encryptors 9-10. --- ### Challenge Task: Now that you're an encryptor expert, let's try making your your own! Create an encryptor function in the style of the ones you've explored so far and name it `new-encryptor`. Now, share it with your classmates to let them take a crack at decrypting it. [Link to shared spreadsheet](https://docs.google.com/spreadsheets/d/1KpU9ZKpVjSDXjmZavgv1F4I9cJcvSn-l/edit?usp=sharing&ouid=105351715786862928191&rtpof=true&sd=true). To share your encryptor in the shared spreadsheet, press 'Publish' in your Pyret file and copy the code that begins with 'import shared-gdrive'. Add that to the sheet. Once you've successfully added yours, try solving another group's from that sheet! **Task 1:** Import another group's encryptor from the encryptor swap spreadsheet using the same format you used to import `support.arr` -- call over a TA if you get stuck. **Task 2:** Write a function called `new-decryptor` that takes in the output of the other group's `new-encryptor` and decrypts it. When you think your decryptor works, consult with the other group. ## Success! Nice! Your new encryptor functions worked! You successfully sent the code and the mission was completed. ------ > Brown University CSCI 0111 (Fall 2024) > **Feedback form:** tell us about your lab experience today! <iframe src="https://forms.gle/52Fi9HdMFRgW1vD67" width="640" height="372" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>