Homework 2: Ads Take Aim

Due: February 7th, 2023 at 11:59PM ET.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

You will need Friday's (Feb 3) lecture to complete most of this assignment. You can do Tasks 1, 2, and 17 onward after Wednesday's (Feb 1) 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.)

Setup

  • Create a new file named hw2-code.arr on Pyret.
  • Unlike with Homework 1, we are writing examples for this assignment! Write examples for each function you implement using a where block. Here is a sample:
fun add-one(n :: Number) -> Number:
  doc: ```Function adds one to the input number```
  n + 1
where:
  add-one(1) is 2
  add-one(3) is 4
  add-one(-1) is 0
end
  • Write examples before you write your code! This will make sure you understand the question (and this habit will help hugely later on).
  • You will be graded on having examples that cover a variety of situations, so take a look at the Testing and Style Guidelines.
  • Do not put your name anywhere in any file.

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!

Getting Help if you Need It

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 the Syllabus's 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.

The Big Picture of this Assignment

Targeted advertising is a real-world example of how data, computation, and social impacts come together. Over the first few 111 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.

The Assignment

Superheroes often have to decipher the ads that they come across to see if there is any suspicious information. Specifically, Batman is being targeted by the Joker with bat-related ads. So, in this assignment, we are going to help Batman by learning how to build functions that determine how ads are targeted.

Part 1: Representing Ads

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:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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):

#|
My calendar includes dedicated times for
- looking for bears
- sleeping ***
- going to class ***
- exploring Rhode Island
|#

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:

TARGET-AGE = 40
TARGET-TOWN = "Rockville"
TARGET-HOBBIES = "running, biking, walking, swimming"

Task 2: Copy and paste these constants into the top of your code.

Part 2: Writing Expressions

Learning Goals:

  • To practice writing expressions.

We're going to practice writing expressions (not functions, we'll get to those later in the assignment).

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 3: Write an expression to determine whether 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").

Task 4: Write an expression that returns 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 5: This time, instead of returning a Boolean, we want to return a String that indicates whether the target town, target hobbies, both, or neither match "Rockville" and "reading" (returning 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 returns the appropriate string given the traits "Rockville" and "reading".

Hint: Remember that if expressions finish with end.

Task 6: 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:

# This is a regular comment.

#| This is a
   multi-line
   comment! 
|#

Part 3: Writing Functions for Targeted Ads

Learning Goals:

  • To practice writing functions and their annotations
  • To practice using documentation

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 7: Convert your expression from Task 3 (checking the age) into a function called within-5. This function should take in a Number representing a person's age and return 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.

Task 8: 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 returns 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).

Task 9:
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 returns a Boolean. Return true if the input is exactly one of "Forest Dale", "Waterville", "Newtown", or "Oldport"; return false otherwise.

Task 10: Now, let's practice using functions to write other functions.

Write the function in-range, which takes in aString representing a person's town and a Boolean representing whether the person has a car. Return true if the place is TARGET-TOWN or both the input place is nearby and the person has a car. Otherwise, the function should return 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 11: 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).

A Different Representation for Ads

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 12: 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 returns a Boolean. This Boolean will be true if any of the following conditions are met, and false otherwise:

  • The customer's age is 35 or younger and the ad contains the word "active"
  • The customer's age is 65 or older and the ad contains the word "healthy"
  • The ad text contains the word "sport"

Comparing different targeting methods

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 13: Write a paragraph comment (in your code file) responding to these questions about the approaches taken in these two functions:

  • How might these functions 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 14: 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:

check "show-ad tests":
  show-ad(...) is false
  ...
end

Part 4: Cleaning Up Code

Learning Goals:

  • To practice reading code
  • To practice cleaning up code by adding local names and/or helper functions

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 15: Rewrite the following function into a new version called ad-charge-clean that meets the Design and Clarity requirements of the CS0111 Style Guidelines.

fun ad-charge-messy(ad-text):
  short-length = 10
  medium-length = 40
  long-length = 70
  if ((string-length(ad-text) >= short-length) and (string-length(ad-text) < medium-length)):
    (string-length(ad-text) * (short-length / 2)) + (string-length(ad-text) * 5)
  else if ((string-length(ad-text) >= medium-length) and (string-length(ad-text) < long-length)):
    (string-length(ad-text) * (medium-length / 2)) + (string-length(ad-text) * 5)
  else if (string-length(ad-text) >= long-length) :
    (string-length(ad-text) * (long-length / 2)) + (string-length(ad-text) * 5)
  else: 0
  end
where:
  ad-charge-messy("Go Bruno!") is 0
  ad-charge-messy("Apply to Brown") is 140
end

Hint: 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 16: 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:

check:
  ad-charge-messy("Go Bruno!") is ad-charge-clean("Go Bruno!")
  ...
end

Check Block (Autograder Compatibility)

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.

check "functions exist and have correct inputs":
  within-5(0)
  hobby-relates("")
  is-nearby("")
  in-range("", true)
  show-ad(0, "", "", true)
  show-ad2("", 0)
  ad-charge-messy("Go Bruno!") is ad-charge-clean("Go Bruno!")
end

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!

Part 5: Obtaining Personal Data

You can do this section without Friday's material

Learning Goals:

  • To learn about ways data are gathered about us through web browsing
  • To think about the beneficial and harmful uses of personal information

this-ad-thinks-it-knows-you

Task 17: 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.
Read the article “These Ads Think They Know You” on the kinds of information that advertisers are able to access and use when targeting ads to users. (You can use this guide to access the New York Times through a free academic pass if needed.)

Task 18: 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 this snapshot (you will use this information in the next task):

ad-personalization information

Task 19: Answer the following prompts in a separate document titled hw2-src.pdf. You can work with either your own ad-matching information or the sample provided above:

  • Identify 3 pieces of information that are inferred about you by Google and two possible ways that Google inferred that information (your answer would look like “Y likely comes from a search I made and X piece of information, X likely comes from A and B, …”).

  • Pick two pieces of ad-matching information (either the same or different from what you used in the last question). For each, describe one possible use which is beneficial or benign, and one which you believe is problematic, unethical, or harmful (your answer could look like: Google knows I like hybrid alternative vehicles; one benefit is X and one harm is Y).

    Reference at least two of the following criteria somewhere in your response:

    • Intent/purpose (academic research vs. targeted ads)
    • If the data is public, does that mean it can be used in any way?
    • Timing (eg. data set from a decade ago)
    • Attributes collected (e.g. those protected by law from discrimination such as religion, race, age, or disability)
    • Inference potential: What might this information help companies infer about you?

Task 20: Turn in your responses to the SRC assignment on Gradescope as a PDF named hw2-src.pdf.

Optional: Similar to Google, Instagram also keeps track of brands and topics you like. To find this list, go to your profile -> the hamburger menu in the top right -> Settings -> Ads -> Ad interests. You can also change your privacy settings from the Ads menu, such as Instagram not using data about you from partners.


Optional Topic: Additional Problem

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.)

Handin

  • Make sure your file is called hw2-code.arr. Hand in your work on Gradescope.
  • When you get to the SRC Component section, make sure you submit a pdf called hw2-src.pdf. Hand in your work on Gradescope to the HW2 SRC submission.
  • You may submit as many times as you want before the deadline. Only your latest submission will be graded (though you will spend late days if the latest submission is after the due date.)

Theme Song

Superman by Taylor Swift


Brown University CSCI 0111 (Spring 2023)