<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, div.dw-content li, div.dw-content li p { font-size: 13pt; margin-bottom: 1em; max-width: 700px; } p 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; } </style>
<h1 id=“assignment-3-sunrise-sunset”>Assignment 3: <a href=“https://yewtu.be/watch?v=03rzUoyq9K0” class=“secret”>Sunrise, Sunset</a></h1>
<p><strong>Assigned</strong>: Thursday, 15 September <br /> <strong>Due</strong>: Wednesday, 21 September, 11:59 p.m.</p>
<p><img src=“https://www.cs.vassar.edu/~cs101/images/DSCF5442.jpeg” style=“max-width: 75%” /></p>
<h2 id=“introduction”>Introduction</h2> <p>We love sunny summer days, but as we move into fall and then winter, we know the hours of daylight begin to dwindle. In this assignment, we’ll use our experience working with tables (in class and in lab) to process a table of solar and lunar temporal information and visualize how the hours of sunlight and moonlight vary over time.</p>
<h2 id=“part-1-data”>Part 1: Data</h2> <p>Some kindly astronomers have shared a data set with us, which they’ve made available as a <a href=“https://docs.google.com/spreadsheets/d/1ci6x3lpCBInsKDCQzdOGPhIMFOZjM9LXwVhnjT8mSoU”>Google sheet</a>.</p> <p><strong>Task</strong>: Copy the unique ID – the long string of letters, numbers, and underscores – in the spreadsheet URL and use it to load it into a Pyret table named
celestial-data
. Refer to the code for loading a table from a spreadsheet, seen in class and <a href=“https://www.cs.vassar.edu/courses/cs101-2022b/labs/03”>in lab</a>.</p>
<p>To ensure the autograder works for your program, use the column names</p> <pre>
year, month, day, age-of-moon, sun-rise, sun-culm, sun-set, moon-rise, moon-culm, moon-set
</pre>
<p>We won’t use <em>all</em> of the columns in this spreadsheet. This is perfectly normal when working with real data sets!</p> <p><strong>Task</strong>: To test the functions we’ll write, we also need a small table of test data. Use the following table or make up your own examples:</p>
<pre>
<var>test-data</var> = <span class="keyword">table</span>: year, month, day, sun-rise, sun-set, moon-rise, moon-set <span class="keyword">row</span>: 2021, 9, 30, <span class="string">"06:30"</span>, <span class="string">"18:30"</span>, <span class="string">"00:00"</span>, <span class="string">"16:00"</span> <span class="keyword">row</span>: 2022, 3, 22, <span class="string">"07:00"</span>, <span class="string">"18:00"</span>, <span class="string">"23:00"</span>, <span class="string">"08:30"</span> <span class="keyword">end</span>
</pre> <p><strong>Warning</strong>: Be sure to write examples in a
where
block for all of the functions you define. (The autograder won’t deduct points for missing tests, but your instructor will!)</p>
<h2 id=“part-2-sunlight”>Part 2: Sunlight</h2>
<p>To compute the hours of sunlight, we’ll need to know the elapsed time between sunrise and sunset. It’s complex to do this with times expressed as hours and minutes, e.g.,
"06:30"
, so we’ll first write a function to convert a time to minutes.</p> <p><strong>Task</strong>: Write a function</p> <pre>
<span class="keyword">fun</span> <span class="name">mins-since-midnight</span>(time :: String) -> Number: ... <span class="keyword">end</span>
</pre> <p>Given a time as a string (like those in the table), it should convert it to the number of minutes since midnight.</p> <p><strong>Hints</strong>:</p> <ul> <li><p>Since the times are strings, you may want to review the <a href=“https://www.pyret.org/docs/latest/strings.html”>documentation</a> listing the functions Pyret provides for working with strings. How could you find just the hours in the string? Just the minutes?</p> <p>Some of the string functions you’ll see in the documentation produce a
List
, which is a data type we haven’t worked with yet. You <em>don’t</em> need to use
List
s to solve this problem!</p></li> <li><p>After you’ve identified the hours and minutes, you’ll need to use the function <a href=“https://www.pyret.org/docs/latest/strings.html#%28part._strings_string-to-number%29”>
string-to-number
</a>. Despite its name, the output is <em>not</em> a number! It’s
some
number if the string can be converted and
none
if it can’t. For this assignment, the strings can always be converted to numbers, so you only need to worry about the some case. To get the number out, use
.value
:</p></li> </ul> <pre>
››› <kbd>string-to-number(<span class="string">"06"</span>)</kbd> some(6) ››› <kbd>string-to-number(<span class="string">"06"</span>).value</kbd> 6
</pre> <p>Now that you can convert the sunrise and sunset times to minutes since midnight, it’s not too hard to compute how many minutes are in between.</p> <p><strong>Task</strong>: Write a function</p> <pre>
<span class="keyword">fun</span> <span class="name">elapsed-mins</span>(start-time :: String, end-time :: String) -> Number: ... <span class="keyword">end</span>
</pre> <p>It should compute the minutes the elapsed between the given start time and end time, provided as strings like those found in the table.</p> <p>And now we’re ready to add a new column to the table containing the hours of sunlight.</p> <p><strong>Task</strong>: Make a table named
celestial-data-sun
that has a new column called
"sunlight"
that contains the number of hours of sunlight for each day.</p> <p>Remember: To do this, you’ll need to define a function that you’ll pass as an input to <a href=“https://www.cs.vassar.edu/~cs101/3/resources/tables.html#build-column”>
build-column
</a>, and it will need to convert the elapsed minutes to <em>hours</em>.</p> <h2 id=“part-3-moonlight”>Part 3: Moonlight</h2> <p>Now that we’ve computed the hours of sunlight, it seems like it should be simple to compute the hours of moonlight – just swap the column names
"sun-rise"
and
"sun-set"
for
"moon-rise"
and
"moon-set"
.</p> <p>This works for some of the rows in our table:</p> <pre>
››› <kbd><var>good-night</var> = test-data.row-n(0)</kbd> ››› <kbd>elapsed-mins(good-night[<span class="string">"moon-rise"</span>], good-night[<span class="string">"moon-set"</span>])</kbd> 960
</pre> <p>But not for others:</p> <pre>
››› <kbd><var>bad-night</var> = test-data.row-n(1)</kbd> ››› <kbd>elapsed-mins(bad-night[<span class="string">"moon-rise"</span>], bad-night[<span class="string">"moon-set"</span>])</kbd> -870
</pre> <p>Why is this? The moon rose at 23:00 (i.e., 11 p.m.) on one day and set at 8:30 a.m. the next.</p> <p>Let’s extend our code to handle this!</p> <p><strong>Task</strong>: Write a function</p> <pre>
<span class="keyword">fun</span> <span class="name">mins-before-midnight</span>(time :: String) -> Number: ... <span class="keyword">end</span>
</pre> <p>Don’t repeat the code from
mins-since-midnight
! Instead, think how you can use a call to that function in your definition. The body of this function only needs to be one line!</p> <p>Now we can improve
elapsed-mins
. Let’s leave our original version (which works for sunlight) and define a new one:</p> <p><strong>Task</strong>: Write a function</p> <pre>
<span class="keyword">fun</span> <span class="name">elapsed-mins-improved</span>(start-time :: String, end-time :: String) -> Number: ... <span class="keyword">end</span>
</pre> <p>When the start time is <em>before</em> the end time, it should work just like
elapsed-mins
.</p> <p>When the start time is <em>after</em> the end time (that is, it’s the next day), the function should instead take the minutes from the start time until midnight (which you just wrote a function to do) and add them to the minutes from midnight until the end time.</p> <p>With
elapsed-mins-improved
, we’re ready to compute the hours of moonlight for each day in the table.</p> <p><strong>Task</strong>: Make a table named
celestial-data-sun-moon
that has a new column called
"moonlight"
that contains the number of hours of moonlight for each day.</p> <p>Be sure you are starting with
celestial-data-sun
from Part 1! We want to end up with a single table that has columns for both hours of sunlight and hours of moonlight.</p>
<h2 id=“part-4-visualization”>Part 4: Visualization</h2> <p>While we can look at the numbers we added to the table, it’s much nicer to get a graphic overview.</p> <p>We’ll draw bar charts, but, unfortunately, none of the columns in the table give a good label for the bars. Since each bar is for one day, they should be labeled with the date.</p> <p><strong>Task</strong>: Make a new table,
celestial-data-to-plot
, that has a column
"date"
giving the abbreviated date for each row, e.g.,
"9/30"
for September 30.</p> <p><strong>Task</strong>: Display bar charts for the hours of sunlight and moonlight. To do this, call</p> <pre>
bar-chart(celestial-data-to-plot, <span class="string">"date"</span>, <span class="string">"sunlight"</span>)
</pre> <p>and</p> <pre>
bar-chart(celestial-data-to-plot, <span class="string">"date"</span>, <span class="string">"moonlight"</span>)
</pre> <p>If everything’s worked right, you should see a very clear trend for the hours of sunlight and a very clear repeating pattern for hours of moonlight!</p>
<h2 id=“assignment-guidelines”>Assignment guidelines</h2> <p>As with Assignment 2, you are expected to follow good Pyret style, including writing docstrings and examples for each function. Review the <a href=“https://www.cs.vassar.edu/~cs101/3/resources/pyret.html”>Testing and Style Guidelines</a> and ask questions if anything’s unclear!</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
asmt03.arr
.</p></li> <li><p>Upload your assignment on <a href=“https://www.gradescope.com”>Gradescope</a>.</p></li> </ol> <p><strong>Note</strong>: You can submit as many times as you want before the deadline. Only your latest submission will be graded.</p> </html>