Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Targeted Ads

ReleasedDue
TBDTBD

Homework 2

Setup

  • Unlike Homework 1, you will need to write tests for your own code. For each functon you implement write examples using a where block

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 skimming through the page before starting the problems!

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 to a diner:

  • Insert ad image

Task 1: Imagine that this ad were posted around town. Write down 4-6 elements in this ad that are designed to attract people to it.

Write your answer as a block comment in your code file. It should look something like the following (but in terms of ads):

#|
My list includes
- item 1
- item 2
- item 3
- item 4
|#

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, we will store the information about the ad in three constants:

TARGET-AGE = 18
TARGET-TOWN = "College Park"
TARGET-LIKES = "waffles, pancakes, sandwiches, omelettes"

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

Part 2: 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 of the constant TARGET-AGE.

Note: When we say “within 5 years,” we mean inclusively.

Task 4: Write an expression that returns true if either "Rockville" is the same as TARGET-TOWN or "salmon" is the same as TARGET-LIKES.

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 food, both, or neither match "College Park" and "salmon" (returning either "town", "food", "both", or "neither"). Use nested if expressions (where one if is inside the body of another if) to write an expression that returns the appropriate string given the traits "College Park" and "salmon".

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

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.

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.

Previously, our expressions checked for an exact match of a food string to TARGET-LIKES. In practice, we might want to match an ad if a given food was within the string (what we call a “substring”), rather than an exact match.

Task 8: Write a function called favorite-food, which takes in a String representing a person’s favorite food and returns a Boolean which is true when TARGET-LIKES contains the input string, and false otherwise.

Hint: Take a look at Pyret’s Strings documentation for an operation on strings that would help you do this.

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.

Task 9: Write in 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 one of "Berwyn Heights", "Hyattsville", "Adelphi", or "Riverdale Park"; return false otherwise.

Now, let’s practice using functions to write other functions.

Task 10: Write in the function in-range, which takes in a String 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 favorite food, 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-LIKES contains their favorite food. The output is false otherwise.

Hint: Try to reuse your work from earlier tasks for this one.

Reminder: Remember to include types, doc strings, and examples with all of your functions.

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: Write 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 22 or younger and the ad contains the word “affordable”
  • The customer’s age is 35 or older and the ad contains the word “convenient”
  • The ad text contains the word “delicious”

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

Note: A check block looks like this

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


Part 4: Cleaning Up Code

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 Terps!") is 0
  ad-charge-messy("Apply to UMD") is 120
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 Terps!") is ad-charge-clean("Go Terps!")
  ...
end


Part 5: Personalized Ads

Task 17: Read “These Ads Think They Know You” on the parts of user information advertisers are able to access and use when targeting ads.

Optional: For a more thorough understanding of how companies collect and utilize user data, read “Your Data Is Shared and Sold… What’s Being Done About It?”.

Task 18: Look at some of the information that Google gathers and infers about its users. You can check your own data by visiting https://myadcenter.google.com/

Task 19: Answer the following prompts in a separate document:

  • Identify 3 pieces of information that Google inferred about you and two possible ways that Google inferred that information (your answer would look like “Y likely came from a search I made about X. X was likely inferred 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, and one which you believe is problematic, unethical, or harmful (your answer could look like: Google knows I like OLED TVs; 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?
    • Time period: Is the data set from a decade ago?
    • Attributes collected (e.g. religion, race, age, or disability)
    • Inference potential: What might this information help companies infer about you?

Task 20: Turn in your response to Gradescope as a PDF.

Handin

  • Turn in both your code and responses on Gradescope