<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; } table { margin: 1em auto; } table td { padding: 1pt 4pt; } div.dw-content a { text-decoration: underline; } div.dw-content p { margin-bottom: 1em; max-width: 700px; } div.dw-content p, div.dw-content td { font-size: 110%; } blockquote { font-size: inherit; } </style>
<h1 id=“lab-2-conditionals-and-reading-code”>Lab 2: Conditionals and Reading Code</h1> <p>9 September 2022</p>
<h2 id=“todays-lab”>Today’s lab</h2> <p>The purpose of this lab is to give you practice:</p> <ul> <li>using Boolean expressions,</li> <li>reading and writing
if
expressions,</li> <li>writing functions that use
if
expressions, and</li> <li>writing examples/tests for the functions you write.</li> </ul>
<p style=“margin: 2em 0; border-top: 1px solid #000; border-bottom: 1px solid #000; padding: 0.5em”><strong>For this lab, you’re encouraged to work with a partner, but it can be completed individually if you prefer.</strong></p>
<h2 id=“part-1-warm-up”>Part 1: Warm up</h2> <p>You’ve been asked to <a href=“https://en.wikipedia.org/wiki/Code_review”>review</a> a program written by a coworker. The problem they were asked to solve was to turn a numeric grade into its corresponding letter grade.</p> <p>Your coworker shows you this solution:</p> <pre>
<span class="keyword">fun</span> <span class="name">grade</span>(percent :: Number) -> String: <span class="keyword">doc</span>: <span class="string">"Return the letter grade for a percent."</span> <span class="keyword">if</span> percent > 0: <span class="string">"F"</span> <span class="keyword">else if</span> percent >= 60: <span class="string">"D"</span> <span class="keyword">else if</span> percent >= 70: <span class="string">"C"</span> <span class="keyword">else if</span> percent >= 80: <span class="string">"B"</span> <span class="keyword">else if</span> percent >= 90: <span class="string">"A"</span> <span class="keyword">end end</span>
</pre>
<p><strong>Task</strong>: Answer the following questions in multi-line Pyret comment, like</p> <pre>
#| This is my answer... continuing ... as long as I need it to! |#
</pre> <ul> <li><p>Write down an example where the code does not return what you think it should.</p></li> <li><p>Why doesn’t this solution work as intended?</p></li> <li><p>Why wouldn't your coworker realize this function isn’t right? What should they have done?</p></li> </ul>
<h2 id=“part-2-weather-alerts”>Part 2: Weather alerts</h2>
<h3 id=“exercise-21-saffirsimpson”>Exercise 2.1: Saffir–Simpson</h3>
<p>Consider making a simple weather app that alerts users of severe weather based on wind speed using the <a href=“https://en.wikipedia.org/wiki/Saffir–Simpson_scale”>Saffir–Simpson scale</a>:</p>
<table> <thead> <tr class=“header”> <th>Category</th> <th>Wind Speed (mph)</th> </tr> </thead> <tbody> <tr class=“odd”> <td>5</td> <td>≥ 157</td> </tr> <tr class=“even”> <td>4</td> <td>130–156</td> </tr> <tr class=“odd”> <td>3</td> <td>111–129</td> </tr> <tr class=“even”> <td>2</td> <td>96–110</td> </tr> <tr class=“odd”> <td>1</td> <td>74–95</td> </tr> <tr class=“even”> <td>Tropical storm</td> <td>39–73</td> </tr> <tr class=“odd”> <td>Tropical depression</td> <td>≤ 38</td> </tr> </tbody> </table>
<p><strong>Task</strong>: Write a function that takes in the average recorded recent wind speed (in miles per hour) and returns the category to which that wind speed belongs:</p>
<pre>
<span class="keyword">fun</span> <span class="name">saffir-simpson</span>(wind-speed :: Number) -> String: ... <span class="keyword">end</span>
</pre>
<p>Here are some examples of the desired behavior of the
saffir-simpson
function:</p>
<pre>
››› <strong>saffir-simpson(65)</strong> <span class="string">"Tropical storm"</span> ››› <strong>saffir-simpson(111)</strong> <span class="string">"Category 3"</span> ››› <strong>saffir-simpson(0)</strong> <span class="string">"Tropical depression"</span>
</pre>
<p>You can assume all inputs are integers (that is, no decimals or fractions). Be sure to write tests for your function that address <em>each</em> category.</p>
<h3 id=“exercise-22-at-vassar”>Exercise 2.2: At Vassar</h3>
<p>For your weather app, you want to get alerts only about weather happening where you are – at Vassar!</p>
<p><strong>Task</strong>: Write the function</p> <pre>
<span class="keyword">fun</span> <span class="name">at-vassar</span>(latitude :: Number, longitude :: Number) -> Boolean: ... <span class="keyword">end</span>
</pre>
<p>It should return true if the specified latitude and longitude correspond roughly to the Vassar campus, with</p> <ul> <li>latitude between 41.656 and 41.693</li> <li>longitude between -73.908 and -73.880</li> </ul> <p>For testing, you may want to use the coordinates</p> <ul> <li>(41.686804, -73.895664): Main Building</li> <li>(34.119108, -118.300388): The Griffith Observatory, Los Angeles</li> </ul> <h3 id=“exercise-23-severe-alert”>Exercise 2.3: Severe alert</h3> <p>For your weather app, you wish to get a severe weather notification if the wind speed is classified as a hurricane (Category 1–5) <em>and</em> the classification is for Vassar.</p> <p><strong>Task</strong>: Write the function</p> <pre>
<span class="keyword">fun</span> <span class="name">severe-alert</span>(wind-speed :: Number, latitude :: Number, longitude :: Number) -> Boolean: ... <span class="keyword">end</span>
</pre> <p>It takes the current wind speed and the location of the wind (latitude and longitude), and returns
true
if the storm is a categorized hurricane (1–5) and over Vassar, and
false
otherwise.</p> <p>Here are some tests you can try (or make your own!):</p> <pre>
<span class="comment"># Hurricane at Main Building</span> severe-alert(190, 41.686804, -73.895664) <span class="keyword">is</span> <span class="constant">true</span> <span class="comment"># Gentler weather at Main Building</span> severe-alert(10, 41.686804, -73.895664) <span class="keyword">is</span> <span class="constant">false</span> <span class="comment"># Hurricane at the Griffith Observatory, Los Angeles</span> severe-alert(190, 34.119108, -118.300388) <span class="keyword">is</span> <span class="constant">false</span>
</pre>
<p>Your
severe-alert
function should <em>call</em> the
at-vassar
function you just wrote!</p>
<h3 id=“exercise-24-extreme-alert”>Exercise 2.4: Extreme alert</h3>
<p>On the other hand, maybe you have a relaxed attitude toward danger, and you only to only get an alert if the storm is the <em>worst</em> type (Category 5) and over Vassar.</p>
<p><strong>Task</strong>: Write the function</p>
<pre>
<span class="keyword">fun</span> <span class="name">extreme-alert</span>(wind-speed :: Number, latitude :: Number, longitude :: Number) -> Boolean: ... <span class="keyword">end</span>
</pre>
<p>It works the same way as the previous function, but only returns
true
if the wind speed is
"Category 5"
.</p>
<p>Consider how this function would differ from
severe-alert
. Can you think of a way you can use the
saffir-simpson
function you’ve already written to avoid duplicate code?</p>
<h2 id=“part-3-code-clean-up”>Part 3: Code clean up</h2>
<p>Oh no – your coworker’s back!</p>
<p>They’ve been asked to write a function to compute the price for a newspaper ad based on the number of characters (that is, letters, numbers, spaces, and punctuation).</p>
<p>This time they’re sure the function works right, and they have test cases to prove it. But, the result is … <em>not</em> pretty:</p>
<pre>
<span class="keyword">fun</span> <span class="name">ad-charge</span>(ad): <var>short-length</var> = 10 <var>medium-length</var> = 40 <var>long-length</var> = 70 <span class="keyword">if</span> ((string-length(ad) >= short-length) <span class="keyword">and</span> (string-length(ad) < medium-length)): (string-length(ad) * (short-length / 2)) + (string-length(ad) * 5) <span class="keyword">else if</span> ((string-length(ad) >= medium-length) <span class="keyword">and</span> (string-length(ad) < long-length)): (string-length(ad) * (medium-length / 2)) + (string-length(ad) * 5) <span class="keyword">else if</span> (string-length(ad) >= long-length) : (string-length(ad) * (long-length / 2)) + (string-length(ad) * 5) <span class="keyword">else</span>: 0 <span class="keyword">end where</span>: ad-charge(<span class="string">"Big Sale!"</span>) <span class="keyword">is</span> 0 ad-charge(<span class="string">"End of Year Clearance"</span>) <span class="keyword">is</span> 210 ad-charge(<span class="string">"End of Year Clearance -- Buy! Buy! Buy! Buy!"</span>) <span class="keyword">is</span> 1100 <span class="keyword">end</span>
</pre>
<p><strong>Task</strong>: Rewrite
ad-charge
to follow good Pyret style (see <a href=“https://www.cs.vassar.edu/~cs101/3/resources/pyret.html”>our guide</a>). Do your best to make the code clear and avoid repetition. But don’t change how it works – the provided test cases should still pass!</p>
<h2 id=“submitting-the-lab”>Submitting the lab</h2> <ul> <li><p>When you’ve completed the exercises, show your code to your instructor or one of the coaches.</p></li> <li><p>Then upload your
lab02.arr
file to the Lab 2 assignment on <a href=“https://www.gradescope.com”>Gradescope</a>. If you worked with a partner, submit a single copy of the lab with both your names. (See these <a href=“https://help.gradescope.com/article/m5qz2xsnjy-student-add-group-members”>instructions</a>.)</p></li> </ul>
<h2 id=“acknowledgments”>Acknowledgments</h2> <p>This lab includes material adapted from Kathi Fisler and colleagues at Brown University.</p> </html>