###### tags: `conditionals` `homeworks` # Homework 2: Ads Take Aim ![](https://marketoonist.com/wp-content/uploads/2014/12/141215.targeted.jpg) ## Setup and Handin ### Setup * Create a new file on [Pyret](https://code.pyret.org/). * 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](https://hackmd.io/@jpg-cs/rkNqSw7ei). * Do **not** put your name anywhere in any file. ### Handin Make sure your file is called hw2-code.arr. Hand in your work on Google Classroom. You may submit as many times as you want before the deadline. Only your latest submission will be graded. ## 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](https://www.pyret.org/docs/latest/strings.html) useful. We recommend briefly browsing this page before working on these problems! ## 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 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 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: The Basics Giselle first needs to be able to determine if a single person fits her advertising criteria. Help Giselle determine if her good friend Colton fits the criteria by writing some **expressions**. 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. `my-name = my-expression`) so it doesn’t print. A. The `TARGET-AGE` is a very specific number, but Giselle wants to target people *around* that age. **Task:** Write an expression to determine if Colton (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.* B. **Task:** Check if **either** Colton’s hometown, `"oatman"`, or his interest, `"reading"`, matches the global variables 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!* C. **Task:** Use at least two nested `if` expressions (one inside the other) to determine if Colton’s age (`21`), hometown (`"oatman"`), and interest (`"reading"`) are the same as all Giselle’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`.* D. **Task:** Complete part C again, but use boolean logic only (`and`, `or`, etc.). This expression should have no `if`s. E. **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 ### 2A: Criteria functions We’ve written some expressions that help us understand if Colton is a good customer for Giselle’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 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](https://www.pyret.org/docs/latest/numbers.html)!* *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 global variable `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`. ### 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 Giselle’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 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 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 Giselle! In fact, it should be clear enough that it meets all of the Design and Clarity requirements of the [Testing and Style Guidelines](https://hackmd.io/@jpg-cs/rkNqSw7ei). 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: Personal Data and Privacy Beyond teaching you technical skills around computing and data, in this course we also want to help you think about 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 issues of computing and data. We’re going to be addressing lots of different topics, starting with data tracking and collection. **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](https://hackmd.io/@jpg-cs/rkkP9rmgi) on how companies can infer information about people based on their online activities. **Task:** Answer the following questions in a paragraph comment within your code file. Label each question with its number. 1. Describe your initial thoughts after reading this article. If this article elicited a reaction from you, explain why you think you had that reaction – and if not, explain why not. 2. Describe a concrete action item from this article that could be relevant to people outside of CS. This could be a (reasonable) behavior to try, a question to ask, etc. Your item should be something you could actually envision many people, or a specific population of people, doing. Be sure to explain why a user might take this action – what specific concern would the action address? These action items will be aggregated and shared anonymously with other students as part of a future assignment. 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. Your answers should be clear and concise, with enough specifics to show that you are thinking about the questions beyond a surface level. ## Part 5: Additional Problem (Optional) For students who want to go a little bit further, [here](https://hackmd.io/@jpg-cs/BkPRpQVxo) 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 Google Classroom for it.