Assignment 2: Ads Take Aim
Due: Thursday September 23, 2021 at 11:59PM ET.
Setup and Handin
- 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
whereblock. 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.
- 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.
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.
The Coding Assignment
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-AGE = 25 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.
Part 1: Targeted ads
1A: Criteria functions
To decide if someone is a good potential customer to advertise to, we'll write a series of predicates (functions that return a
- 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 function called
within-5. This function should take in a
Numberrepresenting a person’s age and return a
TARGET-AGEis within five years of the input, and
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
Note: Check out the Number documentation!
Hint: Make sure to write type annotations, a docstring, and examples first.
- 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
Stringrepresenting a person’s hobby and returns a
truewhen the definition
TARGET-HOBBIEScontains the input hobby in it, and
falseotherwise. It might be useful to take a look at Pyret’s
Stringdocumentation to figure out how to do this.
- Giselle wants to be able to see if a person’s town is within the general area of her store. However,
TARGET-TOWNonly 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
Stringrepresenting a town and returns a
trueif the input is
"topock"(the towns around her store), and
- 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
Stringrepresenting a customer’s town and a
Booleanrepresenting if the person has a car or not. It should return a
trueif the place is
TARGET-TOWNor both the input place is in the target area and the person has a car. Otherwise, the function should return
1B: 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
1C: Using keywords
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
- 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”
Part 2: Clarifying the ad prices
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 end where: 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 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 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.
Part 3: Personal Data and Privacy
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 line comment... |#
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.