<html> <style type=“text/css”> .comment { font-style: italic; color: #c0342d; } .name, .variable, code var, pre var { color: #9e5cb1; } .name { font-weight: bold; } .variable, pre var, code var { font-style: italic; } .keyword { color: #abafb3; } .builtin, .string { color: #417fb8; } .constant { color: #e89f27; } code.pyret { color: black; } table { margin: 1em auto; } table th, table td { padding: 1pt 4pt; font-size: 13pt; } div.dw-content a { text-decoration: underline; } div.dw-content p, div.dw-content li, div.dw-content li p { font-size: 13pt; margin-bottom: 1em; max-width: 750px; } p code, li code { font-size: 11pt; } blockquote { font-size: inherit; } pre kbd, code kbd { background: inherit; color: inherit; box-shadow: none; padding: 0; font: inherit; font-weight: bold; } a.secret { color: inherit; text-decoration: none !important; } a.secret:hover { text-decoration: underline !important; } ol li div.task p:first-child {
margin: 0;
} div.task, p.task {
background-color: #f2d4d7 !important; border: 1px solid #6366a !important; padding: 1em 2.25em; margin: 2em 1.5em;
} p.task, div.task p:first-child {
text-indent: -1.5em;
}
blockquote {
border: 0; margin: 1.5rem;
} </style>
<h1 id=“assignment-2-ads-take-aim”>Assignment 2: Ads Take Aim</h1> <p><strong>Assigned</strong>: Thursday, 8 September <br /> <strong>Due</strong>: Wednesday, 14 September, 11:59 p.m.</p>
<blockquote> <p><img src=“https://www.cs.vassar.edu/~cs101/images/targeted-ad.jpeg” alt=“Targeted advertising cartoon” /></p> <p style=“font-size: smaller”>Joe Dator, <em>The New Yorker</em>, 21 July 2014</p> </blockquote>
<h2 id=“assignment-guidelines”>Assignment guidelines</h2> <ul> <li><p>Create a new file named
asmt02.arr
on <a href=“http://code.pyret.org”>Pyret</a>.</p></li> <li><p>Do not put your name in your assignment; we want to grade anonymously.</p></li> <li><p>You will be graded on following good Pyret style and having examples that cover a variety of situations. Read the <a href=“https://www.cs.vassar.edu/~cs101/3/resources/pyret.html”>Testing and Style Guidelines</a> for details.</p></li> <li><p>Try to write your tests before filling in your functions, so you can quickly tell if they are working correctly.</p></li> </ul>
<h2 id=“help”>Help!</h2> <p>The <a href=“https://www.pyret.org/docs/latest”>Pyret documentation</a> is accessible from the pirate button in the top left corner of <a href=“http://code.pyret.org”>code.pyret.org</a>.</p> <p>Your friendly coaches and professor are here to help with this assignment! Check <a href=“https://calendar.google.com/calendar/embed?wkst=1&bgcolor=%23ffffff&ctz=America%2FNew_York&showCalendars=0&showTz=0&mode=WEEK&showTabs=0&showTitle=0&showNav=1&showDate=0&showPrint=0&src=Y19vczVvYzJvZGYxYXZhOTdtaHVjN2NhMjZvOEBncm91cC5jYWxlbmRhci5nb29nbGUuY29t&color=%23A79B8E”>coaching hours</a> and my <a href=“https://www.cs.vassar.edu/hours”>office hours</a>.</p>
<h2 id=“introduction”>Introduction</h2> <p>Winifred is the owner of a sporting goods store in Poughkeepsie. Recently she’s seen advertisements for a competitor all over town. Afraid of losing business, Winifred hires the Vassar Bros. Ad Agency for help.</p> <p>The agency recommends targeting people living nearby who would be able to get to a store in downtown Poughkeepsie. 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.</p>
<p><strong>Task</strong>: Copy and paste these definitions into the top of your code:</p>
<pre>
<var>TARGET-AGE</var> = 25 <var>TARGET-TOWN</var> = <span class="string">"poughkeepsie"</span> <var>TARGET-HOBBIES</var> = <span class="string">"running, biking, walking, swimming"</span>
</pre>
<p>These definitions are for <em>global variables</em>, which are variables (names) created outside of a function, which are accessible to all functions throughout the program. They are normally defined in the top of the program and are distinguished from local variables by having upper-case names.</p>
<p>Next, let’s help the ad agency figure out what customers fit Winifred’s criteria. We want to write a function that takes a person’s age, hobby, town, and car status and determines whether or not Winifred should send them an ad.</p>
<p>To do this, we first need to figure out more information about them.</p>
<h2 id=“part-1-targeted-ads”>Part 1: Targeted ads</h2>
<h3 id=“criteria-functions”>Criteria functions</h3>
<p>To decide if someone is a good potential customer to advertise to, we’ll write a series of <em>predicates</em> – functions that return the Boolean values
true
or
false
. Make sure to write examples for each function before you try writing the function body.</p> <ol> <li><p>Let’s start by writing a function to see if any potential customer is within five years of Winifred’s target age.</p> <p><strong>Task</strong>: Define the function</p> <pre>
<span class="keyword">fun</span> <span class="name">within-5</span>(age :: Number) -> Boolean: ... <span class="keyword">end</span>
</pre>
<p>It should return
true
if the input,
age
, is within five years of
TARGET-AGE
and
false
if not.</p>
<p>(Check out the <a href=“https://www.pyret.org/docs/latest/numbers.html”>Number documentation</a>!)</p> <p>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
.</p></li>
<li><p>Since Winifred is looking for a variety of interests that customers might have, we want to see if a customer’s interest is <em>one of</em> her target hobbies.</p>
<p><strong>Task</strong>: Define the function</p> <pre>
<span class="keyword">fun</span> <span class="name">hobby-relates</span>(hobby :: String) -> Boolean: ... <span class="keyword">end</span>
</pre>
<p>It should return
true
when the input hobby is contained in
TARGET-HOBBIES
and
false
otherwise.</p>
<p>(Check out the <a href=“https://www.pyret.org/docs/latest/strings.html”>String documentation</a>!)</p></li>
<li><p>Winifred 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.</p>
<p><strong>Task</strong>: Define the function</p>
<pre>
<span class="keyword">fun</span> <span class="name">in-area</span>(town :: String) -> Boolean: ... <span class="keyword">end</span>
</pre>
<p>It should return
true
if the input is
"hyde park"
,
"pleasant valley"
,
"poughkeepsie"
, or
"lagrange"
(the towns around her store), and
false
otherwise.</p>
<p><strong>Hint</strong>: The input
"park"
should return
false
even though
"hyde park"
is one of the towns. Consider: Why is this the case? What makes this different from the task above where we check if global variable
TARGET-HOBBIES
contains a particular hobby?</p></li>
<li><p>Now we can find out if someone lives in the area, but Winifred 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.</p>
<p><strong>Task</strong>: Define the function</p> <pre>
<span class="keyword">fun</span> <span class="name">in-range</span>(town :: String, has-car :: Boolean) -> Boolean: ... <span class="keyword">end</span>
</pre>
<p>The inputs represent a customer’s town and whether they have a car or not. The function should return
true
if the input town is
TARGET-TOWN
or if the input town is in the target area and the person also has a car. Otherwise, the function should return
false
.</p>
<p><strong>Hint</strong>: Can you reuse any of the functions that we already wrote?</p></li> </ol>
<h3 id=“combining-criteria”>Combining criteria</h3>
<p>Now it’s time to put all the criteria together to determine whether an ad should be shown to a potential customer or not!</p> <p><strong>Task</strong>: Define the function</p> <pre>
<span class="keyword">fun</span> <span class="name">show-ad</span>(age :: Number, town :: String, hobby :: String, has-car :: Boolean) -> Boolean: ... <span class="keyword">end</span>
</pre>
<p>The inputs are information about a potential customer, and the output is
true
when</p> <ul> <li>their age is within five years of the target age (inclusive), </li> <li>their town is the
TARGET-TOWN
or they have a car and their town is within the target area, and </li> <li>their hobby is contained in
TARGET-HOBBIES
.</li> </ul> <p>Otherwise, the output is
false
.</p>
<p><strong>Hint</strong>: Consider previous tasks you have completed! Make sure to avoid redundant code or functions that do the same thing!</p>
<h3 id=“using-keywords”>Using keywords</h3>
<p>A different way of targeting ads is looking at keywords in the text of the advertisement rather than specific criteria like Winifred’s target definitions.</p>
<p><strong>Task</strong>: Define the function</p> <pre>
<span class="keyword">fun</span> <span class="name">show-ad2</span>(ad :: String, age :: Number) -> Boolean: ... <span class="keyword">end</span>
</pre> <p>The input ad is the text of an ad and
age
is a person’s age. The function should return
true
if <em>any</em> of the following conditions are met:</p> <ul> <li>The customer’s age is 35 or younger and the ad contains the word “active”,</li> <li>The customer’s age is 65 or older and the ad contains the word “healthy”, or</li> <li>The ad contains the word “sport”.</li> </ul>
<p>Otherwise, it should return
false
.</p>
<h2 id=“part-2-pyret-style-and-testing-check”>Part 2: Pyret style and testing check</h2>
<p>Remember that programs aren’t just written for computers to run; they’re also for people to read. Just as there is good style for writing an English essay, there is good style for writing a program.</p>
<p><strong>Task</strong>: Take a moment to go back through the functions you’ve written:</p> <ul> <li>Did you write a docstring for each function?</li> <li>Do you have examples that demonstrate the functions work as intended?</li> </ul> <p>If not, do so now!</p> <p>Do any of your functions look like this?</p> <pre>
<span class="keyword">if</span> some-question: <span class="constant">true</span> <span class="keyword">else</span>: <span class="constant">false</span> <span class="keyword">end</span>
</pre> <p>This is very common, especially if you’ve used other programming languages before. However, the
if
–
else
is redundant; you could just write
some-question
and get the same result!</p>
<p><strong>Task</strong>: Remove any redundant
if
expressions!</p>
<p><strong>Task</strong>: Copy the following
check
block into the bottom of your program:</p> <pre>
<span class="keyword">check</span> <span class="string">"Functions exist and have correct inputs"</span>: within-5(0) hobby-relates(<span class="string">""</span>) in-area(<span class="string">""</span>) in-range(<span class="string">""</span>, <span class="constant">true</span>) show-ad(0, <span class="string">""</span>, <span class="string">""</span>, <span class="constant">true</span>) show-ad2(<span class="string">""<span>, 0) <span class="keyword">end</span>
</pre>
<p>A
check
block is similar to a
where
block, but not attached to an individual function. This
check
block ensures that you’ve included all the required functions, named correctly, and that they handle inputs in the right order.</p>
<p>If you see this block appear in the interactions window after running it, then you are fine:</p> <p><img src=“https://www.cs.vassar.edu/~cs101/images/check0.png” alt=“Check passed” /></p>
<p>Instead, if you see one of these screens, please check your function names, input types, and input order:</p> <p><img src=“https://www.cs.vassar.edu/~cs101/images/check1.png” alt=“Check failed” /></p> <p><img src=“https://www.cs.vassar.edu/~cs101/images/check2.png” alt=“Check failed” /></p>
<h2 id=“part-3-comparing-approaches”>Part 3: Comparing approaches</h2>
<p>Let’s think a bit about the two approaches we’ve implemented for matching ads to people,
show-ad
and
show-ad2
.</p>
<p><strong>Task</strong>: Respond to the following questions:</p> <ul> <li>How do you think these functions differ from how real ads are targeted?</li> <li>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 targeting ads?</li> </ul> <p>You can put your answers in a multi-line comment</p> <pre>
#| Like this one! |#
</pre> <p>at the bottom of your program.</p>
<p><strong>Task</strong>: In another multiline comment, write at least two test examples that demonstrate weaknesses of
show-ad
and at least two test examples that demonstrate weaknesses of
show-ad2
.</p>
<p>The output from each function for each input should be surprising given the input. That is, an ad could seem perfect for the customer, but the function would output
false
because of a limitation of its design or vice versa. Remember that
ad-text
could be a String containing multiple words!</p>
<h2 id=“part-4-personal-data-and-privacy”>Part 4: Personal data and privacy</h2>
<p>Beyond teaching you technical skills around computing and data, CMPU 101 also wants to help you think about the broader societal issues surrounding them.</p>
<p><strong>Task</strong>: Read the article “<a href=“https://www.nytimes.com/interactive/2019/04/30/opinion/privacy-targeted-advertising.html”>These Ads Think They Know You</a>” on the kinds of information that advertisers are able to access and use when targeting ads to users. (You have free access to the New York Times as a Vassar student. Follow <a href=“https://library.vassar.edu/research-guides/nyt-academic-pass”>these directions</a> to set up your account!)</p>
<p><strong>Task</strong>: Answer the following prompts using <a href=“https://adssettings.google.com/authenticated”>your own Google ad-matching information</a> or – to protect your privacy – the sample shown below (from a real person). Your answers can go in a multiline comment at the bottom of your program.</p>
<p><img src=“https://www.cs.vassar.edu/~cs101/images/asmt2-settings.png” alt=“How your ads are personalized” style=“max-width: 90%”/></p>
<ol> <li><p>Information that a search engine has on you comes from a combination of</p> <ul> <li>your profile,</li> <li>your browsing history, and</li> <li>assumptions that the search engine makes about you based on your browing history.</li> </ul> <p>Identify one piece of ad-matching information of each type. Your answer should look like “<em>X</em> likely comes from the profile, <em>Y</em> likely comes from browsing history, etc.”</p></li> <li><p>Pick one piece of ad-matching information that could have come from multiple sources. Give 2–3 concrete examples of online activities (e.g., “searched for <em>X</em>”) that might have associated that ad-matching label with the account.</p></li> <li><p>Pick two pieces of ad-matching information, which can be the same or different from what you used in the last question. For each, describe</p> <ul> <li>one possible use which is beneficial or benign, and</li> <li>one which you believe is problematic, unethical, or harmful.</li> </ul> <p>Reference at least two of the following criteria somewhere in your response:</p> <ul> <li>Intent/purpose (academic research vs. targeted ads)</li> <li>If the data is public, does that mean it can be used in any way?</li> <li>Timing (e.g., data set from a decade ago)</li> <li>Attributes collected (e.g., those protected by law from discrimination such as religion, race, age, or disability)</li> <li>Inference potential: What might this information help companies infer about you?</li> </ul> <p>There’s 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.</p></li> </ol>
<h2 id=“optional-part-5”>OPTIONAL Part 5</h2>
<p><strong>This part is entirely optional and will not be graded, but you may find it rewarding to try!</strong></p> <p>When we use a search engine, ads related to those searches sometimes appear in the same window. That suggests that the content of our search terms (also known as “queries”) might be used to select ads. For this problem, you will work with a simple version of this setup.</p>
<p>Assume that an ad is an arbitrary String and that a query can have up to three words (such as “bike”, “store”, “vassar”). We want to rate an ad for how well it fits these three words. We will score each word against the ad. A word contributes a score of 1 if it (or a simple modification to it) appears in the ad; otherwise it contributes 0. The rating for an ad against the whole query is the sum of the ratings for each of the three query words divided by the length of the ad String (in characters).</p> <p>For this question, we will consider modifications that make a word singular (by removing an “s” from the end, e.g.,
"helmets"
→
"helmet"
) or turn a gerund into a verb (by removing “ing” from the end, e.g.,
"skateboarding"
→
"skateboard"
). This means that any of the query words can score against an ad with or without an ending removed.</p>
<p><strong>Task:</strong> Complete the following function:</p> <pre>
<span class="keyword">fun</span> <span class="name">rating</span>(ad :: String, wd1 :: String, wd2 :: String, wd3 :: String) -> Number: ... <span class="keyword">end</span>
</pre>
<p>Your work should emphasize two skills:</p> <ul> <li>How to structure your code. The computation itself is less important than how you organize your code (using named computations, helper functions, etc.).</li> <li>A set of good examples for any functions you write for this problem.</li> </ul> <p>This question doesn’t earn any points. It is meant to let you fine-tune your skills at structuring code and developing good sets of examples to illustrate a problem.</p>
<h2 id=“submitting-the-assignment”>Submitting the assignment</h2>
<ol> <li><p>Download your file (<em>File</em> → <em>Download</em>) and ensure it’s named
asmt02.arr
.</p></li> <li><p>Upload your assignment on <a href=“https://www.gradescope.com”>Gradescope</a>.</p></li> </ol> <p>Part 1 will be automatically graded when you submit your assignment, but your final assignment grade will be determined by your instructor when they review your work, including checking the programming style (Part 2) and reading your answers for Part 3 and Part 4.</p> <p>You can submit as many times as you want before the deadline. Only your latest submission will be graded.</p>
<h2 id=“acknowledgments”>Acknowledgments</h2>
<p>This assignment is adapted from Kathi Fisler and colleagues at Brown University.</p> </html>