Homework 2: Rad Ads

Due: Tuesday, February 4, 2020 at 9:00PM EST.

Setup and Handin

Setup

Once you begin your implementation, Pyret will also run your tests on your own code.

NOTE: Pyret can only provide this feedback for tests you write in your tests.arr file. You may write test-cases for your helper functions in your code.arr file, but they will not be automatically checked for validity and thoroughness. Pyret will therefore always tell you VALIDITY UNKNOWN and THOROUGHNESS UNKNOWN for tests you put in your code.arr file.

Handin

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

The Assignment

In the sleepy ol’ town of Oatman, AZ, there lives a shoemaker, and her name is Giselle. After a long day of stitching leather and molding soles, there’s nothing that grinds Giselle’s gears quite like coming home to a big ol’ pile of irrelevant ads on her doorstep. One evening, she finds herself in this exact situation. She wants to understand how this could happen.

Part 1: Targeted ads

Giselle wants to figure out which ads will target her. She knows that most ads will care about how old she is, what she does for work, and where she lives. These values are represented in the global constants AGE, LIVES-IN, and JOB, as defined at the top of the document.

1A: Criteria functions

First, help Giselle write functions to see if she fits certain criteria for ad-makers by comparing potential ads’ target values with the global constants.

  1. Task: To determine whether a target age is within five years of Giselle’s age, fill in the function within-5. This function should take in a Number representing a target age and return a Boolean which is true if Giselle’s 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 Giselle, the function should return true.

    Note: Check out the Number documentation!

  2. Task: Fill in the function works-with, which takes in a String representing the ad’s target “job base” and returns a Boolean which is true when Giselle’s job contains the input 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.

    Task: Underneath the function, add a comment (1-2 sentences) explaining why works-with is a useful function to create. What is the purpose of it?

    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! 
    |#
    
  3. Ads often target certain types of places, like cities or rural regions. Giselle wants to be able to see if her location matches with a targeted place type. However, LIVES-IN only gives us a specific place! To solve this issue, we need to write a function to check if certain places are cities or not before we can check whether an ad targets Giselle’s place type.

    Task: Fill in the function is-town, which takes in a String representing a town and returns a Boolean which is true if the input is "abilene", "durango", "oatman", or "dallas", and false otherwise.

    Now we can use is-town in another function to determine if an ad targets Giselle’s place type.

    Task: Fill in the function place-type-matches, which takes in a String representing a targeted place type, and returns a Boolean. If this input is "town", then return true if Giselle lives in a town (use is-town() for this!), and false if not. If the input is anything besides "town", we can assume the place type matches and return true.

1B: Combining criteria

Now it’s time to put all the criteria together to determine whether an ad will target Giselle or not! We will do this using the function show-ad(). The inputs are a Number representing a target age, a String representing a target job area, and a String representing a target place type. The output is a Boolean which is true when Giselle’s age is within five years of the target age, her job contains the target job area, and she lives in the same place type as the target place type. The output is false otherwise.

Before filling in the actual function, we want to figure out what types of inputs should produce true or false. Just like with every function, it’s really useful to write the tests before implementing the function.

Task: Fill in the check block in the test document by writing tests for show-ad(). Upon running tests for this function in the code file, you will receive feedback on whether your tests are comprehensive.

Task: Fill in the show-ad() function. You do not need to add tests within the function using where, because we will grade the tests in the check block instead.

1C: Using keywords

A different way of targeting ads is looking at keywords in the text of the advertisement.

Task: Fill in the function show-ad2(), which takes in a String representing the text of an ad and returns a Boolean. This Boolean will be true if any of the following conditions are met, and false otherwise:

Hint: you might want to use one of your criteria functions from section 1A!

1D: Comparing different targeting methods

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

Task: In a check block, for each ad-placing method, make up at least two different “advertisements” which demonstrate the weaknesses of the method, for a total of four advertisement examples. An advertisement for show-ad() would be a combination of a target age, job area, and place type, and an advertisement for show-ad2() would be the text of the advertisement.

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

Part 2: Clarifying the ad prices

Giselle decides to place an ad herself! However, the code for pricing is really unclear. As a result, she can’t figure out how much an ad will cost based on the length of its text.

The code works just fine, 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
  end
where:
  ad-charge("Go Bruno!") is 0
  ad-charge("Apply to Brown") is 140
end

Task: Help Giselle figure out what’s going on by editing the code so it’s clearer and cleaner (put a copy in your code file, then edit it – you don’t need to include the original). In fact, it should be clear enough that it meets all of the CS0111 Style Guidelines! It’s important to make sure, though, that the code still works the way it originally did.

Part 3: Personal Data and Privacy

Beyond teaching you technical skills around computing and data, CS111 also wants to help you think about some of the broader societal issues surrounding them. To this end, we will have a few short reading assignments that ask you to reflect on the implications and possible ethical designs of computing and data.

Task: Read this short article on how companies can infer information about people based on their online activities. (You can use this guide to access the New York Times through a free academic pass if needed.)

Task: Answer the following question in a paragraph comment within your code file.

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.

Theme Song

Giselle, Act II: pas de deux composed by Adolphe Adam


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