Homework 2: Rad Ads
Due: Tuesday, February 4, 2020 at 9:00PM EST.

Setup and Handin
Setup
-
Unlike with Homework 1, we are writing tests for this assignment! This is the link to the template for this homework. It will open a modified version of Pyret with a tab for your tests, and (when you’re ready) a tab for your implementation.
Note: The testing file for this homework is only for one function, show-ad()
. For every other function, as per the Style Guide, use where
for testing, and write these tests in the code file.
-
We recommend writing tests in the testing file before beginning your implementation. When you run your tests, you’ll get feedback about their validity and thoroughness:
- A test suite is valid if it’s consistent with the problem statement. For instance, the test
show-ad(0) is true
is invalid, because the problem statement says show-ad
takes three parameters, not one. If the editor says your test suite is INVALID, you should re-read the problem statement and fix the invalid test.
- A valid test suite is thorough if it’s really good at catching buggy implementations. To evaluate thoroughness, we run your test suite against a bunch of buggy implementations. Try to catch as many
y implementations as you can!
Once you begin your implementation, Pyret will also run your tests on your own code.
NOTE: Pyret can only provide this feedback for tests you write in your tests.arr
file. You may write test-cases for your helper functions in your code.arr
file, but they will not be automatically checked for validity and thoroughness. Pyret will therefore always tell you VALIDITY UNKNOWN and THOROUGHNESS UNKNOWN for tests you put in your code.arr
file.
-
After writing your tests, fill out the stencil code in the code file as you work through the assignment. Once you are finished with the assignment, the tests you have written in the tests file should all pass.
-
Do not put your name anywhere in any file.
Handin
- Download both your solutions file and your testing file (File > Download) and make sure they are called
hw2-code.arr
and hw2-tests.arr
respectively. Hand in your work on Gradescope, making sure to use your anonymous account.
- You may submit as many times as you want before the deadline. Only your latest submission will be graded.
Helpful Things
Documentation
The Pyret documentation is accessible from the Pirate button in the top left corner of code.pyret.org.
For this assignment, you will find the Strings documentation useful. We recommend briefly browsing this page before working on these problems!
Staff Assistance
Your friendly TAs and Professor are here to help with this assignment! You can find our schedule for office hours here.
If you have any questions, please post on Campuswire. Make sure you’re following the Course Missive’s guidelines for Campuswire, including not posting any code.
The Assignment
In the sleepy ol’ town of Oatman, AZ, there lives a shoemaker, and her name is Giselle. After a long day of stitching leather and molding soles, there’s nothing that grinds Giselle’s gears quite like coming home to a big ol’ pile of irrelevant ads on her doorstep. One evening, she finds herself in this exact situation. She wants to understand how this could happen.
Part 1: Targeted ads
Giselle wants to figure out which ads will target her. She knows that most ads will care about how old she is, what she does for work, and where she lives. These values are represented in the global constants AGE
, LIVES-IN
, and JOB
, as defined at the top of the document.
1A: Criteria functions
First, help Giselle write functions to see if she fits certain criteria for ad-makers by comparing potential ads’ target values with the global constants.
-
Task: To determine whether a target age is within five years of Giselle’s age, fill in the function within-5
. This function should take in a Number
representing a target age and return a Boolean
which is true
if Giselle’s age is within five years of the input, and false
if not.
This should be an inclusive comparison, meaning that if the target age is exactly five years older or younger than Giselle, the function should return true
.
Note: Check out the Number documentation!
-
Task: Fill in the function works-with
, which takes in a String
representing the ad’s target “job base” and returns a Boolean
which is true
when Giselle’s job contains the input in it, and false
otherwise. It might be useful to take a look at Pyret’s String
documentation to figure out how to do this.
Task: Underneath the function, add a comment (1-2 sentences) explaining why works-with
is a useful function to create. What is the purpose of it?
Hint: You can create multi-line comments when you go over 80 characters on one line!
# This is a regular comment.
#| This is a
multi-line
comment!
|#
-
Ads often target certain types of places, like cities or rural regions. Giselle wants to be able to see if her location matches with a targeted place type. However, LIVES-IN
only gives us a specific place! To solve this issue, we need to write a function to check if certain places are cities or not before we can check whether an ad targets Giselle’s place type.
Task: Fill in the function is-town
, which takes in a String
representing a town and returns a Boolean
which is true
if the input is "abilene"
, "durango"
, "oatman"
, or "dallas"
, and false
otherwise.
Now we can use is-town
in another function to determine if an ad targets Giselle’s place type.
Task: Fill in the function place-type-matches
, which takes in a String
representing a targeted place type, and returns a Boolean
. If this input is "town"
, then return true
if Giselle lives in a town (use is-town()
for this!), and false
if not. If the input is anything besides "town"
, we can assume the place type matches and return true
.
1B: Combining criteria
Now it’s time to put all the criteria together to determine whether an ad will target Giselle or not! We will do this using the function show-ad()
. The inputs are a Number
representing a target age, a String
representing a target job area, and a String
representing a target place type. The output is a Boolean
which is true
when Giselle’s age is within five years of the target age, her job contains the target job area, and she lives in the same place type as the target place type. The output is false
otherwise.
Before filling in the actual function, we want to figure out what types of inputs should produce true
or false
. Just like with every function, it’s really useful to write the tests before implementing the function.
Task: Fill in the check
block in the test document by writing tests for show-ad()
. Upon running tests for this function in the code file, you will receive feedback on whether your tests are comprehensive.
Task: Fill in the show-ad()
function. You do not need to add tests within the function using where
, because we will grade the tests in the check
block instead.
1C: Using keywords
A different way of targeting ads is looking at keywords in the text of the advertisement.
Task: Fill in the function show-ad2()
, which takes in a String
representing the text of an ad and returns a Boolean
. This Boolean
will be true
if any of the following conditions are met, and false
otherwise:
- Giselle is 35 or younger and the ad contains the word “young”
- Giselle is 65 or older and the ad contains the word “retire”
- Giselle lives in a town and the ad contains the word “nightclub”
Hint: you might want to use one of your criteria functions from section 1A!
1D: Comparing different targeting methods
Task: The functions show_ad and show_ad2 are our first attempt at writing functions to match ads to people. The next couple of homeworks will revisit this theme as we learn more programming concepts. Write a paragraph comment (in your code file) responding to these questions about the approaches taken in these two functions:
- How might they differ from how real ads are rated?
- Think about the way we set up the code and the programming operations that we used. What are the limitations of our current code/operations for evaluating ads? What would you want to be able to do in code to do a better job of rating ads?
Task: In a check block, for each ad-placing method, make up at least two different “advertisements” which demonstrate the weaknesses of the method, for a total of four advertisement examples. An advertisement for show-ad()
would be a combination of a target age, job area, and place type, and an advertisement for show-ad2()
would be the text of the advertisement.
The output from each function for each input should be surprising given the input: for example, an ad could seem perfect for Giselle, but the function would output false
because of a limitation of its design.
Part 2: Clarifying the ad prices
Giselle decides to place an ad herself! However, the code for pricing is really unclear. As a result, she can’t figure out how much an ad will cost based on the length of its text.
The code works just fine, but it looks like this:
fun ad-charge(text):
short-length = 10
medium-length = 40
long-length = 70
if ((string-length(text) >= short-length) and (string-length(text) < medium-length)):
(string-length(text) * (short-length / 2)) + (string-length(text) * 5)
else if ((string-length(text) >= medium-length) and (string-length(text) < long-length)):
(string-length(text) * (medium-length / 2)) + (string-length(text) * 5)
else if (string-length(text) >= long-length) :
(string-length(text) * (long-length / 2)) + (string-length(text) * 5)
else: 0
end
where:
ad-charge("Go Bruno!") is 0
ad-charge("Apply to Brown") is 140
end
Task: Help Giselle figure out what’s going on by editing the code so it’s clearer and cleaner (put a copy in your code file, then edit it – you don’t need to include the original). In fact, it should be clear enough that it meets all of the CS0111 Style Guidelines! It’s important to make sure, though, that the code still works the way it originally did.
Part 3: Personal Data and Privacy
Beyond teaching you technical skills around computing and data, CS111 also wants to help you think about some of the broader societal issues surrounding them. To this end, we will have a few short reading assignments that ask you to reflect on the implications and possible ethical designs of computing and data.
Task: Read this short article on how companies can infer information about people based on their online activities. (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 within your code file.
- Describe a concrete action item from this article that could be relevant to people outside of CS. This could be a (reasonable) behavior to try, something to do, something to ask, etc. Your item should be something you could actually envision many people doing. You can focus on a specific population of people if you like. The goal is to think about practical implications for users of technology (don’t worry – we’ll get to implications for the technologists themselves in the coming weeks).
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.
Theme Song
Giselle, Act II: pas de deux composed by Adolphe Adam
Brown University CSCI 0111 (Spring 2020)
Do you have feedback? Fill out this form.
Homework 2: Rad Ads
Due: Tuesday, February 4, 2020 at 9:00PM EST.
Setup and Handin
Setup
Unlike with Homework 1, we are writing tests for this assignment! This is the link to the template for this homework. It will open a modified version of Pyret with a tab for your tests, and (when you’re ready) a tab for your implementation.
Note: The testing file for this homework is only for one function,
show-ad()
. For every other function, as per the Style Guide, usewhere
for testing, and write these tests in the code file.We recommend writing tests in the testing file before beginning your implementation. When you run your tests, you’ll get feedback about their validity and thoroughness:
show-ad(0) is true
is invalid, because the problem statement saysshow-ad
takes three parameters, not one. If the editor says your test suite is INVALID, you should re-read the problem statement and fix the invalid test.Once you begin your implementation, Pyret will also run your tests on your own code.
NOTE: Pyret can only provide this feedback for tests you write in your
tests.arr
file. You may write test-cases for your helper functions in yourcode.arr
file, but they will not be automatically checked for validity and thoroughness. Pyret will therefore always tell you VALIDITY UNKNOWN and THOROUGHNESS UNKNOWN for tests you put in yourcode.arr
file.After writing your tests, fill out the stencil code in the code file as you work through the assignment. Once you are finished with the assignment, the tests you have written in the tests file should all pass.
Do not put your name anywhere in any file.
Handin
hw2-code.arr
andhw2-tests.arr
respectively. Hand in your work on Gradescope, making sure to use your anonymous account.Helpful Things
Documentation
The Pyret documentation is accessible from the Pirate button in the top left corner of code.pyret.org.
For this assignment, you will find the Strings documentation useful. We recommend briefly browsing this page before working on these problems!
Staff Assistance
Your friendly TAs and Professor are here to help with this assignment! You can find our schedule for office hours here.
If you have any questions, please post on Campuswire. Make sure you’re following the Course Missive’s guidelines for Campuswire, including not posting any code.
The Assignment
In the sleepy ol’ town of Oatman, AZ, there lives a shoemaker, and her name is Giselle. After a long day of stitching leather and molding soles, there’s nothing that grinds Giselle’s gears quite like coming home to a big ol’ pile of irrelevant ads on her doorstep. One evening, she finds herself in this exact situation. She wants to understand how this could happen.
Part 1: Targeted ads
Giselle wants to figure out which ads will target her. She knows that most ads will care about how old she is, what she does for work, and where she lives. These values are represented in the global constants
AGE
,LIVES-IN
, andJOB
, as defined at the top of the document.1A: Criteria functions
First, help Giselle write functions to see if she fits certain criteria for ad-makers by comparing potential ads’ target values with the global constants.
Task: To determine whether a target age is within five years of Giselle’s age, fill in the function
within-5
. This function should take in aNumber
representing a target age and return aBoolean
which istrue
if Giselle’s age is within five years of the input, andfalse
if not.This should be an inclusive comparison, meaning that if the target age is exactly five years older or younger than Giselle, the function should return
true
.Note: Check out the Number documentation!
Task: Fill in the function
works-with
, which takes in aString
representing the ad’s target “job base” and returns aBoolean
which istrue
when Giselle’s job contains the input in it, andfalse
otherwise. It might be useful to take a look at Pyret’sString
documentation to figure out how to do this.Task: Underneath the function, add a comment (1-2 sentences) explaining why
works-with
is a useful function to create. What is the purpose of it?Hint: You can create multi-line comments when you go over 80 characters on one line!
Ads often target certain types of places, like cities or rural regions. Giselle wants to be able to see if her location matches with a targeted place type. However,
LIVES-IN
only gives us a specific place! To solve this issue, we need to write a function to check if certain places are cities or not before we can check whether an ad targets Giselle’s place type.Task: Fill in the function
is-town
, which takes in aString
representing a town and returns aBoolean
which istrue
if the input is"abilene"
,"durango"
,"oatman"
, or"dallas"
, andfalse
otherwise.Now we can use
is-town
in another function to determine if an ad targets Giselle’s place type.Task: Fill in the function
place-type-matches
, which takes in aString
representing a targeted place type, and returns aBoolean
. If this input is"town"
, then returntrue
if Giselle lives in a town (useis-town()
for this!), andfalse
if not. If the input is anything besides"town"
, we can assume the place type matches and returntrue
.1B: Combining criteria
Now it’s time to put all the criteria together to determine whether an ad will target Giselle or not! We will do this using the function
show-ad()
. The inputs are aNumber
representing a target age, aString
representing a target job area, and aString
representing a target place type. The output is aBoolean
which istrue
when Giselle’s age is within five years of the target age, her job contains the target job area, and she lives in the same place type as the target place type. The output isfalse
otherwise.Before filling in the actual function, we want to figure out what types of inputs should produce
true
orfalse
. Just like with every function, it’s really useful to write the tests before implementing the function.Task: Fill in the
check
block in the test document by writing tests forshow-ad()
. Upon running tests for this function in the code file, you will receive feedback on whether your tests are comprehensive.Task: Fill in the
show-ad()
function. You do not need to add tests within the function usingwhere
, because we will grade the tests in thecheck
block instead.1C: Using keywords
A different way of targeting ads is looking at keywords in the text of the advertisement.
Task: Fill in the function
show-ad2()
, which takes in aString
representing the text of an ad and returns aBoolean
. ThisBoolean
will betrue
if any of the following conditions are met, andfalse
otherwise:Hint: you might want to use one of your criteria functions from section 1A!
1D: Comparing different targeting methods
Task: The functions show_ad and show_ad2 are our first attempt at writing functions to match ads to people. The next couple of homeworks will revisit this theme as we learn more programming concepts. Write a paragraph comment (in your code file) responding to these questions about the approaches taken in these two functions:
Task: In a check block, for each ad-placing method, make up at least two different “advertisements” which demonstrate the weaknesses of the method, for a total of four advertisement examples. An advertisement for
show-ad()
would be a combination of a target age, job area, and place type, and an advertisement forshow-ad2()
would be the text of the advertisement.The output from each function for each input should be surprising given the input: for example, an ad could seem perfect for Giselle, but the function would output
false
because of a limitation of its design.Part 2: Clarifying the ad prices
Giselle decides to place an ad herself! However, the code for pricing is really unclear. As a result, she can’t figure out how much an ad will cost based on the length of its text.
The code works just fine, but it looks like this:
Task: Help Giselle figure out what’s going on by editing the code so it’s clearer and cleaner (put a copy in your code file, then edit it – you don’t need to include the original). In fact, it should be clear enough that it meets all of the CS0111 Style Guidelines! It’s important to make sure, though, that the code still works the way it originally did.
Part 3: Personal Data and Privacy
Beyond teaching you technical skills around computing and data, CS111 also wants to help you think about some of the broader societal issues surrounding them. To this end, we will have a few short reading assignments that ask you to reflect on the implications and possible ethical designs of computing and data.
Task: Read this short article on how companies can infer information about people based on their online activities. (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 within your code file.
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.
Theme Song
Giselle, Act II: pas de deux composed by Adolphe Adam