Assignment 2: Ads Take Aim

Due: Thursday September 23, 2021 at 11:59PM ET.

Targeted Advertising

  • Create a new file on Pyret.
  • Unlike with Assignment 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
  add-one(1) is 2
  add-one(3) is 4
  add-one(-1) is 0
  • 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.
  • Make sure your file is called asmt2-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.

The Pyret documentation is accessible from the Pirate button in the top left corner of

For this assignment, you will find the strings documentation useful. We recommend briefly browsing this page before working on these problems!

Your friendly coaches and professor are here to help with this assignment! Check your section's homepage for information on office and coaching hours.

If you have any questions, please post on Campuswire. Make sure you’re following the syllabus's guidelines for Campuswire, including not posting any code.

Giselle has been the proud owner of a sporting goods store in Oatman for the past few months, but recently she has seen advertisements for a competitor all over town. Afraid of losing much of her business, Giselle hires the Oatman Ad Agency in her 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 definitions into the top of your code.

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

Let’s help the Ad Agency figure out what customers fit Giselle’s criteria. We want to write a function that takes a person’s age, hobby, town, and car status and determines whether or not Giselle 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.

To decide if someone is a good potential customer to advertise to, we'll write a series of predicates (functions that return a Boolean).

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

    Task: Write a the 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 Giselle is looking for a variety of interests that customers might have, we want to see if a customer’s interest is one of her 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 definition TARGET-HOBBIES contains the input hobby 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.

  3. Giselle wants to be able to see if a person’s town is within the general area of her 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 "kingman", "needles", "oatman", or "topock" (the towns around her store), and false otherwise.

  4. Now we can find out if someone lives in the area, but Giselle knows that people can only get to her 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 a String 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.

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.

A different way of targeting ads is looking at keywords in the text of the advertisement rather than specific criteria like Giselle’s target definitions.

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”

Giselle finally places her ad with the Oatman Ad Agency! However, the code for pricing is really unclear, so she 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(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
  ad-charge("Go VC!") is 0
  ad-charge("GoBrewers!") is 100
  ad-charge("GoBrewers!GoBrewers!GoBrewers!GoBrewers!") is 1000
  ad-charge("GoBrewers!GoBrewers!GoBrewers!GoBrewers!GoBrewers!GoBrewers!GoBrewers!") is 2800

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 Giselle! In fact, it should be clear enough that it meets all of the Design and Clarity requirements of the 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 four 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.

Beyond teaching you technical skills around computing and data, CMPU 101 also wants to help you think about the broader societal issues surrounding them.

Goal: Students should begin to think about how data collection impacts technology users regardless of users’ technical knowledge, and articulate ways in which issues in technology can cause different people to react.

Task: Read this short article on computational inference on how companies can infer information about people based on their online activities.

Using a multi-line comment

... multi

in your program, describe your thoughts about the reading and how real online tracking compares with the simple version implemented in this assignment. Your response should be approximately one paragraph. There are no right or wrong answers here. Our goal is to get you thinking about the context of the technical content of the course.

  • We've enabled the autograder for this assignment.
  • This is a trial run.
  • Be sure to use the filename (asmt2-code.arr) and function names exactly as indicated below.
  • If you do not, the autograder will not work.
  • Please let us know if you encounter anything out of the ordinary.

This lab includes material adapted from Kathi Fisler and colleagues at Brown University.