You will need Friday's (2/2) lecture to complete most of this assignment. You can do Tasks 1-3, as well as start on tasks 11-12, after Wednesday's (1/31) class. (Normally, we will finish covering what you need by the time homework is released; we're just a touch off due to the start of the semester.)
hw2-code.arr
in Pyret.where
block. Here is a sample: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!
You can post small questions on Edstem. Check the FAQ that will be pinned on Edstem first, as some questions may have already been asked and answered. Make sure you're following our guidelines for Edstem, including not posting any (close to) solution code publically.
If you have a longer question or want to go over something with a friendly staff member, come to office hours .
Targeted advertising is a real-world example of how data, computation, and social impacts come together. Over the first few assignments, we want you to develop an understanding of how these systems work. The coding components of the first three homeworks will explore different ways of matching ads to potential customers, where the methods get more realistic as we go (and highlight some important program design points).
This week, we start with the simplest form of matching ads: writing explicit conditional expressions to see whether a person is in the target audience for a single, fixed ad.
Frogs often have to decipher the ads that they come across to see if there is any swimming or hopping information. Specifically, Sailor Frog is being targeted by the Pirate Frog with treasure hunting related ads. So, in this assignment, we are going to help Sailor Frog by learning how to build functions that determine how ads are targeted.
The first step in processing something with a program is to represent that thing in terms of focused, discrete pieces of data. Consider the following ad for an outdoor adventure meetup:
Task 1: Imagine that this ad was posted around town. Write down a list of 6-8 elements that are in this ad that are designed to attract people to it. Then, put stars next to those elements that you think a program might use to decide whether to show the ad to someone.
Write your answer as a block comment in your code file. It should look something like the following (but in terms of ads, not calendars):
Task 2:
Data plays a big 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.
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.
In a separate document that you will save as hw2-src.pdf, write your responses to the following question(s). Using your data gathered by Google, only if you’re comfortable, what is the most surprising thing about your targeted ads? In 2-3 sentences, discuss how Google may have come to infer those assertions about you and how this information might be used when making ad targeting decisions.
Note: If you don't have your Google personalized ads turned on, use the information on the right most phone in the image given to answer the second question.
Note: A summary of the researched examples submitted in Task 2 will be shared after submission and grading. This may include paraphrased versions of your response. Everything will remain anonymous. If you do NOT wish to share your response in any capacity, please write “DO NOT SHARE” at the beginning of the document.
Task 3: For this assignment, we will use three pieces of information to represent an ad: the age of person it targets, the location of the event, and the kinds of activities it involves. Because we are working with a single fixed ad this week, we will store the information about the ad in three constants (values that we will access in the expressions that we write using their names):
Copy and paste these constants into the top of your code.
Learning Goals:
We're going to practice writing expressions (not functions, we'll get to those later in the assignment). It might seem a little strange to write down an expression where you can see what the answer is, but we are starting with these warmup tasks in order to transition to reasoning about functions in the later tasks. Think of this task as writing out a full mathematical expression in place of an answer, like 2 * 2
instead of 4
, but with programming! The expressions should refer to the names above (TARGET-AGE
, TARGET-TOWN
, TARGET-HOBBIES
) instead of using their values.
When your expressions are evaluated, they might print out in the interactions window when you hit "Run". That's fine (for this assignment), or you can give the expression a name (e.g., task1 = ...
)`
Task 4: Write an expression to that evaluates to true if and only if Anthony (Age 21) is within 5 years (inclusive) of the constant TARGET-AGE
.
Hint: You can use the >
and <
symbols to compare two values with exclusive bounds (think "greater than") and >=
and <=
symbols to compare two values with inclusive bounds (think "greater than or equal to"). You might have to combine two comparisons with a Boolean operator (such as and
or or).
Task 5: Write an expression that evaluates to true if either "Providence"
is the same as TARGET-TOWN
or "reading"
is the same as TARGET-HOBBIES
.
Note: You can you use ==
or string-equal
to do the comparison (the former works on any type of data, the latter works only on strings).
Task 6: This time, instead of producing a Boolean, we want to produce a String that indicates whether the target town, target hobbies, both, or neither match "Rockville"
and "reading"
(evaluating to either "town"
, "hobbies"
, "both"
, or "neither"
). Use nested if
expressions (where one if
is within the answer of the other) to write an expression that evaluates to the appropriate string given the traits "Rockville"
and "reading"
.
Hint: Remember that if
expressions finish with end
.
Task 7: Contrast the expressions for the previous two tasks (the one that returned the Boolean
and the one that returned a String
). Write 1-2 sentences in a comment comparing nested if
expressions and boolean logic. When can we use each form? When must we use each form?
Note: You can create multi-line comments when you go over 80 characters on one line as follows:
Learning Goals:
So far, our expressions have fixed the information about a person to compare to the constants about the ad. In order to check whether the ad matches an arbitrary person, we need to write functions that take information about the person as inputs.
Note: Remember to include types, doc-strings, and examples with all of your functions! Refer to the Testing and Style Guidelines if you need help with this!
Task 8: Convert your expression from Task 4 (checking the age) into a function called within-5
. This function should take in a Number
representing a person's age and produces a Boolean
which is true
if target TARGET-AGE
is within five years of the input, and false
if not. Make sure to write type annotations on the input parameters, a docstring, and examples.
Note: for this task and the next few tasks, you should be able to re-use a lot of the logic from tasks 4-7. Think about the examples from class: what specific value from the expression can you replace with an input name to make the computation work on any value input?
Task 9: Previously, our expressions checked for an exact match of a hobbies string to TARGET-HOBBIES
. In practice, we might want to match an ad if a given hobby was within the string (what we call a "substring"), rather than an exact match.
Write a function called hobby-relates
, which takes in a String
representing a person's hobby and produces a Boolean
which is true
when TARGET-HOBBIES
contains the input hobby string, and false
otherwise. Take a look at Pyret's Strings documentation for an operation on strings that would help you do this (practicing using documentation is a key goal of this question, and is an important skill to develop in general).
Task 10: Consider Sporty Frog who has hobbies that include “mountain biking”, “polo”, and “swiming” (which they misspelled by accident). In your code file under hobby-relates
write a multi-line comment about why each of these hobbies might lead to a misleading output from hobby-relates
.
Task 11: Take a look at this article and then read this article about Facebook ads and ad targeting options, such as zip code and gender. In your hw2-src.pdf file, write 4-5 sentences that discuss how these options, even if unintentional, could lead to harmful outcomes.
Task 12: The article above provides just one example of issues that may have been avoided had more diverse voices been included in the decision making process. Research another issue (having to do with programming or otherwise) that may have been prevented with more intentional efforts for diversity, equity, and inclusion. Write about your researched example in your hw2-src.pdf file - feel free to be creative with presenting the issue and how it could have been avoided. A good starting point would be to think about how your own background or identity might lead to you to make limiting assumptions or overlook certain edge cases. There is no specific length of response we are looking for, but make sure to cite your sources, summarize the issue, and include your own views and experiences.
Note: A summary of the researched examples submitted in Task 12 will be shared after submission and grading. This may include paraphrased versions of your response. Everything will remain anonymous. If you do NOT wish to share your response in any capacity, please write “DO NOT SHARE” at the beginning of the document.
Task 13: Our current approach to matching towns/locations for ads is a bit restrictive: someone who lives in a neighboring town might still be able to get there. This motivates our next function.
Write the function is-nearby
, which takes in a String
representing the name of a town and produces a Boolean
. Produce true
if the input is exactly one of "Forest Dale"
, "Waterville"
, "Newtown"
, or "Oldport"
; produce false
otherwise.
Task 14: Now, let's practice calling functions while writing the body of another function.
Write the function in-range
, which takes in aString
representing a person's town and a Boolean
representing whether the person has a car. Produce true
if the place is TARGET-TOWN
or both the input place is nearby and the person has a car. Otherwise, the function should produce false
.
Now it's time to put all the criteria together to determine whether an ad should be shown to a potential customer or not.
Task 15: Write a function called show-ad
. The inputs are a Number
representing a customer's age, a String
representing their town, a String
representing their hobby, and a Boolean
representing whether they have a car. The output is a Boolean
which is true
when their age is within five years of the target age (inclusive), their town is within the area with a car or in TARGET-TOWN
itself, and TARGET-HOBBIES
contains their hobby. The output is false
otherwise.
Hint: Try to reuse your work from earlier tasks on this assignment (that's part of the point of this sequence of examples).
So far, we have represented an ad as three constants (TARGET-AGE
, etc). What if we represented each ad as a single string with all of its text instead? What other ways of matching people and ads might we try?
Task 16: Fill in the function show-ad2
, which takes in a String
representing the text of an ad and a Number
representing a person's age, and produces a Boolean
. This Boolean
will be true
if any of the following conditions are met, and false
otherwise:
Task 17: Task 11 includes a real world example that shows how functions can produce unexpected or incorrect outcomes for certain individuals/groups. However, even when a function itself is “perfectly accurate,” it can be used in harmful ways that we will continue to explore throughout the course. The function show-ad2
makes assumptions about the customers that these ads are targeting. In a multi-line comment under show-ad2
, list out at least two of these assumptions (consider the relationship between age and health, and what the conditions might be leaving out).
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, and our functions will become more sophisticated.
Task 18: Write a multi-line comment (in your code file) responding to these questions about the approaches taken in these two functions:
Task 19: In a check
block (just like a where
block but not attached to any particular function), write two examples using each ad-placing method (show-ad
and show-ad2
). Each example should show a situation in which the function would output 'false' (because of a limitation of the function), even though the ad would otherwise seem a good match for the person.
A check
block looks like:
Learning Goals:
In this exercise, we want you to use three concepts we've learned–-local names, helper functions, and examples–-to make a piece of code easier to read without changing what it computes.
Task 20: Rewrite the following function, which computes the cost of publishing an ad based on the length of the ad text, into a new version called ad-charge-clean
that meets the Design and Clarity requirements of the CS0111 Style Guidelines.
Hint: Part of the point of this exercise is to figure out what the code is doing. You can either copy the code and edit it to clean up or try writing parts from scratch: follow whichever makes the most sense to you. Try looking for repeated or similar expressions: repeated expressions turn into locally-named ones, while similar expressions get turned into helper functions.
Task 21: We want to make sure that the clean version produces the same output as the original messy one. Provide a check
block of the following form to convince yourself that your two versions are consistent in their outputs:
When you have completed all the tasks of this homework, please copy the following check block into the bottom of your code file! The check block will be checking that all required functions are included, correctly named, and that they handle inputs in the right order. Please note that it does not check or confirm if the body of your function is correct.
If you see the block below appear in the interactions window after running it, then you are fine! Please submit to Gradescope.
If not, double-check your function names, input types, and input order. If you are stuck at any point, please feel free to come to hours or post on Ed!
For students who want to go a little bit further, here is an additional problem. This is completely optional and will be submitted separately. If you don't complete this question, feel free to ignore the Gradescope assignment for it.
(This is NOT part of the required extra work for those interested in going directly to CS200.)
hw2-code.arr
. Make sure your responses for Tasks 2, 11, and 12 are in a separate PDF file titled hw2-src.pdf
. Hand both in on Gradescope. Because we only see the latest submission, make sure that you submit both files when you are done.Frog 🐸 (Five Little Speckled Frogs) by Super Simple Songs.
Brown University CSCI 0111 (Spring 2024)