Homework 3: How The Tables Have Turned

Due: Tuesday February 11, 2020 at 9:00PM EST.
Setup and Handin
Setup
-
This is the link to the template for this homework! One file is for your assignment, and one is for your testing.
-
You will be graded for comprehensive testing. If you can’t figure out what you’re forgetting to test, don’t spend too much time trying to find every bug. You should write tests in the tests doc as instructed below before beginning your implementation. Also, please come talk to us at TA hours and we can help think through comprehensive testing.
-
Do not put your name anywhere in the file.
-
We will finish the material for Part 1 and Part 3 on Wednesday (Feb 5th); Part 2 will depend on material from Friday (Feb 7th).
Handin
- Download both your solutions file and your testing file and make sure they are called
hw3-code.arr
and hw3-tests.arr
respectively. Hand in your work on Gradescope, making sure to use your anonymous account.
Remember your resources!
Useful Functions
-
not
is a builtin function that takes a Boolean, and inverts it: not(true)
→ false
and not(false)
→ true
-
==
is an equality test; 3 == 3
→ true
and 3 == 4
→ false
.
The Assignment
Part 1: Nicknames
After placing an ad in Homework 2, Giselle has fallen in love with the marketing industry. She decides to start her own ad agency! Realizing that any proper business has a team, she hires a posse of Livia, Floria, Colton, and Monica. To establish fun vibes in the workplace, Giselle decides to give nicknames to her gang.
She formats each nickname as a String
in a specific way by pairing a real name and a nickname with diamond brackets and a comma. Even though this is the Wild West, she has some strict conditions for which nicknames are allowed. Help her figure out which nickname options are valid!
Requirements for a valid nickname pair:
- The first and last characters must be
<
and >
respectively
- Valid:
"<floria,bandit>"
- Invalid:
"floria,bandit>"
, "floria,bandit"
- How to check this: What characters of the
String
are we looking at? What function can look at a particular character of a String
?
- Have a comma as its middle character, meaning there must be the same number of characters on either side of it
- Valid:
"<livia,eagle>"
- Invalid:
"<livia,robber>"
, "<livia eagle>"
, "<livia, eagle>"
(the space adds a character to the right side)
- How to check this: What does this tell us about the length of the string? Can it be even? (Take a look at the provided function
is-even
in hw3-code.arr
) How can we check that the middle character is a comma? Start by finding the index of the middle character.
Hint: The function num-floor() might be useful.
- Either (or both) the name or the nickname must contain the lowercase letter “a”
- Valid:
"<colton,bandit>"
- Invalid:
"<colton,doctor>"
- How to check this: Take a look at Homework 2 and see how you checked if a
String
was contained in another String
.
- The name or nickname CANNOT contain the sequence
curse
- Floria is very superstitious!
- Valid:
"<acu,rse>"
(the comma interrupts the sequence)
- Invalid:
"<monica,curser>"
Task: First, write tests in the hw3-tests.arr
doc for nickname-check
. The function takes in a nickname pair String
and returns true
if the pair follows the rules, and false
if it does not. Be sure to write comprehensive tests to examine the behavior of the function across various types of acceptable input. Of the 7 broken solutions (the bugs you are trying to catch), 5 of them are from errors in nickname-check
. The other 2 will be caught in Part 2. Do not use the given examples below as part of your testing.
Example tests (to go into the check block):
nickname-check("<livia,queen>") is true
nickname-check("<livia bandit") is false
Task: Now, fill in the function nickname-check
in the file hw3-code.arr
. Since you already tested this function in the other document, you do not need a where
block for this function. Make sure to include a doc string and a type annotation.
Note: Being able to look in language documentation for useful operations is an important skill, which is why we aren’t telling you exactly which String
operations to use. We want you to look at the String
documentation to find useful operations for solving this problem. However, limit yourself to operations with input and output types that we have used this semester (Number
, String
, Boolean
). Don’t use operations that return List
, as we haven’t covered that yet.
Part 2: More sophisticated ad matching
Following in the footsteps of her former foes, Giselle wants to make targeted ads. She realizes she can sell more ads by improving how she matches ads to users. In particular, she wants to consider more information than someone’s age, what kind of work they do, and what sort of place they live in. Now, she also wants to consider (in an anachronistic way) how much someone uses a mobile phone (“never”, “sometimes”, or “frequently”) and whether they ride horses.
If we updated our show-ad
function from Homework 2, we’d get a function header like:
fun show-ad(target-age :: Number, job-area :: String,
place-type :: String, mobile-usage :: String,
rides-horses :: Boolean) -> Boolean:
...
end
How messy! If next week she added another collection of criteria, this would become even more unwieldy. Having the criteria as their own inputs doesn’t scale. We need a way to enable growing the set of attributes without making a mess of our code.
Instead of passing the criteria for each ad as a separate input, what if we somehow bundled the infomation about the ad into one piece of data? For example, we could make a Table
with one row that has the information about the ad which we’re checking against the constants that describe Giselle. For example:

Now, our show-ad
function can look like:
fun show-ad(ad-info :: Table) -> Boolean:
...
end
Part 2A: Taking into account new parameters
Note: It is very important that you make sure to use the CS0111 Table documentation, not the official Table
documentation.
Now we will implement the changes to show-ad
using the new definition which takes in a Table
containing five columns and one row.
Task: Copy your show-ad
function from your hw2-code.arr
document into your hw3-code.arr
document (you can find your hw2-code.arr file in the code.pyret.org folder within your Google Drive).
Then, edit show-ad
by doing the following:
Task: Change the input to a one-row table and edit the function body accordingly. The input table could be any table with one row and the five expected columns. Make sure you can get the code to run by writing some tests (you will need to explicitly create several tables with one row each to use as test cases). Feel free to fix any issues from last week’s code in the process.
Task: We need to check whether the horses match (i.e. if someone rides horses and the ad wants horse-riders). Should this be its own function or should you just check it directly in show-ad
? What are the advantages/disadvantages of each? Write a comment below show-ad
answering these questions.
We’re going to use helper functions for this.
Task: Fill in the horses-match
function, which takes in a Boolean and returns true if Giselle’s horse status matches that of the ad. Check whether the horses match.
Task: Fill in the check-phone-usage
function, which takes in a String representing the desired phone usage of an advertiser. The input String must be either “frequently”, “sometimes”, or “never”. The function should return true if Giselle’s phone usage is at least as high as what the ad wants. For example, if Giselle’s phone usage is “never”, the function should return true if the input is “never”, and false if it is “sometimes” or “frequently”. If the input is not one of the three acceptable strings, simply return false.
Make sure your show-ad
function uses horses-match
and check-phone-usage
along with other helper functions.
Part 2B: Excluding what the ad doesn’t care about
Sometimes, the ads that we are checking don’t care about all of the possible attributes of someone. For example, we might not care about someone’s age if we just want to sell a lasso to people living in Oatman.
Specifically, how might you create an ad-table for an ad that doesn’t care about someone’s job area or target age? One idea is to leave the table cell blank. For the purposes of this assignment, we consider a String
to be blank if it is an empty string (which we write as “”) and a Number
to be blank if it is negative.
Task: Write comprehensive test cases for show-ad
in hw3-tests.arr
(2 of the 7 bugs will be for show-ad
). Take into account both the changes in Part 2A (new parameters) and the possible “blank” values. You can feel free to copy your tests from hw2-tests.arr
and edit accordingly.
Task: Modify your code for show-ad
to handle blank values for job area and target age. The challenge here is to figure out when and how to handle these blanks. You do not need to handle blank cases for anything besides job area and target age.
Task: Could you use this same technique to handle whether someone rides horses? Why or why not? Are there other downsides to taking this approach? Write another comment under show-ad
answering these questions.
Part 3: These Ads Think that They Know You
Building on this assignment’s code and last assignment’s ethical readings, we are asking you to read an article and reflect on the implications and possible ethical designs of computing and data.
Task: Read this short article on how information about every part of our lives can be used not just to target us but to invisibly manipulate others for economic and political ends. (You can use this guide to access the New York Times through a free academic pass if needed.)
Task: Answer the following question in a paragraph comment at the bottom of your code file.
Reflect on the online ads that you usually encounter, and think about the personal information that surveillance companies might have been collecting from you. To you, what factors should ethical data collection take into consideration? Consider the following:
- Intent/purpose (academic research vs. targeted ads)
- Timing (eg. data set from a decade ago)
- If the data is public, does that mean it can used in any ways?
There is no right or wrong answer here. Our goal is to get you thinking about the context of the technical content of the course. Your answer should be clear and concise, with enough specifics to show that you are thinking about the question beyond a surface level.
Task: Please add a comment at the bottom of your code file with the approximate number of hours you spent on this homework. This will be used only to average how long the homeworks are taking students this semester, and it is completely anonymous. Thank you for working to help us improve this class as we grow!
Theme Song
Turning Tables by Adele
Brown University CSCI 0111 (Spring 2020)
Do you have feedback? Fill out this form.
Homework 3: How The Tables Have Turned
Due: Tuesday February 11, 2020 at 9:00PM EST.
Setup and Handin
Setup
This is the link to the template for this homework! One file is for your assignment, and one is for your testing.
You will be graded for comprehensive testing. If you can’t figure out what you’re forgetting to test, don’t spend too much time trying to find every bug. You should write tests in the tests doc as instructed below before beginning your implementation. Also, please come talk to us at TA hours and we can help think through comprehensive testing.
Do not put your name anywhere in the file.
We will finish the material for Part 1 and Part 3 on Wednesday (Feb 5th); Part 2 will depend on material from Friday (Feb 7th).
Handin
hw3-code.arr
andhw3-tests.arr
respectively. Hand in your work on Gradescope, making sure to use your anonymous account.Remember your resources!
Table
documentation)Useful Functions
not
is a builtin function that takes a Boolean, and inverts it:not(true)
→false
andnot(false)
→true
==
is an equality test;3 == 3
→true
and3 == 4
→false
.The Assignment
Part 1: Nicknames
After placing an ad in Homework 2, Giselle has fallen in love with the marketing industry. She decides to start her own ad agency! Realizing that any proper business has a team, she hires a posse of Livia, Floria, Colton, and Monica. To establish fun vibes in the workplace, Giselle decides to give nicknames to her gang.
She formats each nickname as a
String
in a specific way by pairing a real name and a nickname with diamond brackets and a comma. Even though this is the Wild West, she has some strict conditions for which nicknames are allowed. Help her figure out which nickname options are valid!Requirements for a valid nickname pair:
<
and>
respectively"<floria,bandit>"
"floria,bandit>"
,"floria,bandit"
String
are we looking at? What function can look at a particular character of aString
?"<livia,eagle>"
"<livia,robber>"
,"<livia eagle>"
,"<livia, eagle>"
(the space adds a character to the right side)is-even
inhw3-code.arr
) How can we check that the middle character is a comma? Start by finding the index of the middle character.Hint: The function num-floor() might be useful.
"<colton,bandit>"
"<colton,doctor>"
String
was contained in anotherString
.curse
- Floria is very superstitious!"<acu,rse>"
(the comma interrupts the sequence)"<monica,curser>"
Task: First, write tests in the
hw3-tests.arr
doc fornickname-check
. The function takes in a nickname pairString
and returnstrue
if the pair follows the rules, andfalse
if it does not. Be sure to write comprehensive tests to examine the behavior of the function across various types of acceptable input. Of the 7 broken solutions (the bugs you are trying to catch), 5 of them are from errors innickname-check
. The other 2 will be caught in Part 2. Do not use the given examples below as part of your testing.Example tests (to go into the check block):
nickname-check("<livia,queen>") is true
nickname-check("<livia bandit") is false
Task: Now, fill in the function
nickname-check
in the filehw3-code.arr
. Since you already tested this function in the other document, you do not need awhere
block for this function. Make sure to include a doc string and a type annotation.Note: Being able to look in language documentation for useful operations is an important skill, which is why we aren’t telling you exactly which
String
operations to use. We want you to look at theString
documentation to find useful operations for solving this problem. However, limit yourself to operations with input and output types that we have used this semester (Number
,String
,Boolean
). Don’t use operations that returnList
, as we haven’t covered that yet.Part 2: More sophisticated ad matching
Following in the footsteps of her former foes, Giselle wants to make targeted ads. She realizes she can sell more ads by improving how she matches ads to users. In particular, she wants to consider more information than someone’s age, what kind of work they do, and what sort of place they live in. Now, she also wants to consider (in an anachronistic way) how much someone uses a mobile phone (“never”, “sometimes”, or “frequently”) and whether they ride horses.
If we updated our
show-ad
function from Homework 2, we’d get a function header like:How messy! If next week she added another collection of criteria, this would become even more unwieldy. Having the criteria as their own inputs doesn’t scale. We need a way to enable growing the set of attributes without making a mess of our code.
Instead of passing the criteria for each ad as a separate input, what if we somehow bundled the infomation about the ad into one piece of data? For example, we could make a
Table
with one row that has the information about the ad which we’re checking against the constants that describe Giselle. For example:Now, our
show-ad
function can look like:Part 2A: Taking into account new parameters
Note: It is very important that you make sure to use the CS0111 Table documentation, not the official
Table
documentation.Now we will implement the changes to
show-ad
using the new definition which takes in aTable
containing five columns and one row.Task: Copy your
show-ad
function from yourhw2-code.arr
document into yourhw3-code.arr
document (you can find your hw2-code.arr file in the code.pyret.org folder within your Google Drive).Then, edit
show-ad
by doing the following:Task: Change the input to a one-row table and edit the function body accordingly. The input table could be any table with one row and the five expected columns. Make sure you can get the code to run by writing some tests (you will need to explicitly create several tables with one row each to use as test cases). Feel free to fix any issues from last week’s code in the process.
Task: We need to check whether the horses match (i.e. if someone rides horses and the ad wants horse-riders). Should this be its own function or should you just check it directly in
show-ad
? What are the advantages/disadvantages of each? Write a comment belowshow-ad
answering these questions.We’re going to use helper functions for this.
Task: Fill in the
horses-match
function, which takes in a Boolean and returns true if Giselle’s horse status matches that of the ad. Check whether the horses match.Task: Fill in the
check-phone-usage
function, which takes in a String representing the desired phone usage of an advertiser. The input String must be either “frequently”, “sometimes”, or “never”. The function should return true if Giselle’s phone usage is at least as high as what the ad wants. For example, if Giselle’s phone usage is “never”, the function should return true if the input is “never”, and false if it is “sometimes” or “frequently”. If the input is not one of the three acceptable strings, simply return false.Make sure your
show-ad
function useshorses-match
andcheck-phone-usage
along with other helper functions.Part 2B: Excluding what the ad doesn’t care about
Sometimes, the ads that we are checking don’t care about all of the possible attributes of someone. For example, we might not care about someone’s age if we just want to sell a lasso to people living in Oatman.
Specifically, how might you create an ad-table for an ad that doesn’t care about someone’s job area or target age? One idea is to leave the table cell blank. For the purposes of this assignment, we consider a
String
to be blank if it is an empty string (which we write as “”) and aNumber
to be blank if it is negative.Task: Write comprehensive test cases for
show-ad
inhw3-tests.arr
(2 of the 7 bugs will be forshow-ad
). Take into account both the changes in Part 2A (new parameters) and the possible “blank” values. You can feel free to copy your tests fromhw2-tests.arr
and edit accordingly.Task: Modify your code for
show-ad
to handle blank values for job area and target age. The challenge here is to figure out when and how to handle these blanks. You do not need to handle blank cases for anything besides job area and target age.Task: Could you use this same technique to handle whether someone rides horses? Why or why not? Are there other downsides to taking this approach? Write another comment under
show-ad
answering these questions.Part 3: These Ads Think that They Know You
Building on this assignment’s code and last assignment’s ethical readings, we are asking you to read an article and reflect on the implications and possible ethical designs of computing and data.
Task: Read this short article on how information about every part of our lives can be used not just to target us but to invisibly manipulate others for economic and political ends. (You can use this guide to access the New York Times through a free academic pass if needed.)
Task: Answer the following question in a paragraph comment at the bottom of your code file.
Reflect on the online ads that you usually encounter, and think about the personal information that surveillance companies might have been collecting from you. To you, what factors should ethical data collection take into consideration? Consider the following:
There is no right or wrong answer here. Our goal is to get you thinking about the context of the technical content of the course. Your answer should be clear and concise, with enough specifics to show that you are thinking about the question beyond a surface level.
Task: Please add a comment at the bottom of your code file with the approximate number of hours you spent on this homework. This will be used only to average how long the homeworks are taking students this semester, and it is completely anonymous. Thank you for working to help us improve this class as we grow!
Theme Song
Turning Tables by Adele