Try   HackMD

Homework 2: Ads Take Aim

Due: Tuesday September 21, 2021 at 9:00PM 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 →

Setup and Handin

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:
  n + 1
where:
  add-one(1) is 2
  add-one(3) is 4
  add-one(-1) is 0
end
  • Try to write your examples before filling in your functions so you can quickly tell if they are working correctly.
  • 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.

Handin

  • Make sure your file is called hw2-code.arr. Hand in your work on Gradescope.
  • 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.)

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 Edstem. Make sure you're following the Course Missive's guidelines for Edstem, including not posting any code.

The Coding Assignment

Pedro has been the proud owner of a sporting goods store in Oatman for the past few months, but recently he has seen advertisements for a competitor all over town. Afraid of losing much of his business, Pedro hires the Oatman Ad Agency in town.

The Oatman Ad Agency recommends targeting people living nearby who would be able to get to a store in downtown Oatman. They decide specifically to target people with sporting interests in their twenties. The Ad Agency already has data on locals' age, hobbies, town, and whether they have a car.

Task: Copy and paste these global variables into the top of your code.

TARGET-AGE = 25
TARGET-TOWN = "oatman"
TARGET-HOBBIES = "running, biking, walking, swimming"

Let's help the Ad Agency figure out what customers fit Pedro's criteria. We want to write a function that takes a person's age, hobby, town, and car status and determines whether or not Pedro should send them an ad.

In order to figure out whether or not someone should get an ad, we need to first figure out some more basic information about them.

Part 1: The Basics

Learning Goals:

  • To practice writing expressions

Pedro first needs to be able to determine if a single person fits his advertising criteria. Help Pedro determine if his good friend Erick fits the criteria by writing some expressions (not functions!).

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. `m

In this section, all expressions should evaluate to Booleans (either true or false).

Note: true is not the same as "true"!

Task: The TARGET-AGE is a very specific number, but Pedro wants to target people around that age. Write an expression to determine if Erick (age 21) is within 5 years of age of the global variable 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"). When we say "within 5 years," we mean inclusively.

We will cover the remaining tasks in this section on Friday

Task: Check if either Erick's hometown, "oatman", or his interest, "reading", match exactly the global variables TARGET-TOWN and TARGET-HOBBIES representing the targets of the adverstisment.

Note: This should be done in one line and evaluate to a Boolean. You can you use == to compare two values. Also, capitalization matters when comparing two strings!

Task: Use at least two nested if expressions (one inside the other) to determine if Erick's age (21), hometown ("oatman"), and interest ("reading") are the same as all Pedro's criteria determined by the global variables TARGET-AGE, TARGET-TOWN, and TARGET-HOBBIES. This means his age is within 5 years of the TARGET-AGE and the hometown and interests are the same String as their targets.

Hint: Remember to end all your if expressions with end.

Task: Complete the previous task again, but use boolean logic only (and, or, etc.). This expression should have no ifs.

Task: Write 1-2 sentences in a comment comparing nested if expressions and boolean logic. Which do you find more clear? Which do you think is easier to edit?

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! 
|#

Part 2: Targeted ads

Learning Goals:

  • To practice writing functions

2A: Criteria functions

We've written some expressions that help us understand if Erick is a good customer for Pedro's ad, but what if we want to check this for any customer? Instead of rewriting these expressions for each person, we can generalize this by writing functions.

  1. Let's start by writing a function to see if any potential customer is within 5 years of Pedro's target age.

    Task: Write 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.

    This should be an inclusive comparison, meaning that if the target age is exactly five years older or younger than the target, the function should return true.

    Note: Check out the Number documentation!
    Hint: Make sure to write type annotations, a docstring, and examples first.

  2. Since Pedro is looking for a variety of interests that customers might have, we want to see if a customer's interest is one of his target hobbies.

    Task: Write a function called hobby-relates, which takes in a String representing a person's hobby and returns a Boolean which is true when the global variable TARGET-HOBBIES contains the input hobby in it, and false otherwise. Take a look at Pyret's String documentation for an operation on strings that would help you do this (practicing using documentation is a key goal of this question).

We will cover the remaining tasks in section 2 on Friday

  1. Pedro wants to be able to see if a person's town is within the general area of his store. However, TARGET-TOWN only gives us a specific place! To solve this issue, we need to write a function to check if a certain location is in the target town or its surrounding area.

    Task: Write in the function in-area, which takes in a String representing a town and returns a Boolean which is true if the input is equal to "kingman", "needles", "oatman", or "topock" (the towns around his store), and false otherwise.

  2. Now we can find out if someone lives in the area, but Pedro knows that people can only get to his store if they live in the target town itself or if they can drive to the target town from another town in the area.

    Task: Write in the function in-range, which takes in aString representing a customer's town and a Boolean representing if the person has a car or not. It should return a Boolean which is true if the place is TARGET-TOWN or both the input place is in the target area and the person has a car. Otherwise, the function should return false.

2B: Combining criteria

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: 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 if 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.

2C: Using keywords

A different way of targeting ads is looking at keywords in the text of the advertisement rather than specific criteria like Pedro's target variables.

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

2D: 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: 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: In a check block (just like a where block but not attached to any particular function!), for each ad-placing method (show-ad and show-ad2), write at least two different examples which demonstrate the weaknesses of the method, for a total of four examples.

The output from each function for each input should be surprising given the input: for example, an ad could seem perfect for the customer, but the function would output false because of a limitation of its design.

Part 3: Clarifying the ad prices

We'll finish covering the constructs in this code on Friday, but we did the cleanup aspects on Wednesday

Pedro finally places his ad with the Oatman Ad Agency! However, the code for pricing is really unclear, so he can't figure out how much an ad will cost based on the length of its text. 😤

The code is completely functional, but it looks like this:

fun ad-charge(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("Go Bruno!") is 0
  ad-charge("Apply to Brown") is 140
end

Take a minute to read the code all the way through and figure out what it's doing. Try working through one of the examples provided to see how the answer was derived. This will make it a lot easier to do the task.

Task: Rewrite the code so it's clearer and cleaner in order to help Pedro! In fact, it should be clear enough that it meets all of the Design and Clarity requirements of the CS0111 Style Guidelines. Feel free to copy and edit it, but it might be easier to just start from scratch.

It's important to make sure that the code still works the way it originally did.

Note: Feel free to copy over the same two examples from the original version of this function. It's important to have some examples to make sure the code still functions the same way, but we're not grading your examples here. Instead, we're just grading your style.

Part 4: Obtaining Personal Data

You can do this section without Friday's material

Throughout the course, we are also going to study how the programming content we are learning interacts with social aspects of how technology gets used or abused.

Task: 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: Look at some of the information that Google gathers and infers about users. You can check your own ad settings by visiting https://adssettings.google.com/authenticated, or look at this snapshot (from a real user):

ad-personalization information

Task: Answer the following prompts in a paragraph comment at the bottom of your code file. You can work with either your own ad-matching information or the sample provided above:

  • Information that a search engine has on you comes from a combination of your profile, your browsing history, and assumptions that the search engine makes about you based on your browing history. Identify one piece of ad-matching information of each type. (your answer would like look like "X likely comes from the profile, Y likely comes from browsing history, ")

  • Pick one piece of ad-matching information that could have come from multiple sources. Give 2-3 concrete examples of online activities (i.e., "searched for ") that might have associated that ad-matching label with your account (or the sample user's account).

  • 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. 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?

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.


Part 5: Additional Problem (Optional)

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.

If you are considering going to CS18 after this course (taking the "bridge"), this is NOT part of the required extra work.

Theme Song

Undertale OST: 022 - Snowdin Town Composed by Toby Fox


Brown University CSCI 0111 (Fall 2021)
Do you have feedback? Fill out this form.