10 September 2021
The purpose of this lab is to give you practice:
if
expressionsif
expressions
Write an if
expression that produces a greeting based on the current
time. For this exercise, the constant TIME
can be assumed to contain the
current time, expressed as military time,
e.g., 9:00 a.m. is the number 0900
and 6:00 p.m. is 1800
.
If the time is between 5 a.m. and 12 noon, the greeting should be “Good morning”
.
If it's past noon but before 6 p.m., it should be “Good afternoon”
. Otherwise, it
should be “Good night”
.
Note: if
expressions don't need to occur within functions. You only
need a function if the computation depends on an input that's provided
when the function is called. For example, this is valid code:
number = 111 if number == 111: "Yay" else: "Nay" end
Your friend came up with an implementation, but they're having some issues. Consider their code:
TIME = 0900 if TIME >= 0500: "Good morning" else if (TIME >= 1200) and (TIME <= 1800): "Good afternoon" else: "Good night" end
Write down an example where the code does not return what you think it should.
Consider making a simple weather app that alerts users of severe weather based on wind speed using the Saffir–Simpson scale.
Write a function saffir-simpson(wind-speed :: Number) → String
, that
takes in the average recorded recent wind speed (in miles per hour) and
returns the category to which that wind speed belongs. For this function, you
can assume all inputs are integers (no decimal numbers).
Saffir-Simpson Scale | |
---|---|
Category | Wind Speed (mph) |
5 | $ \geq $ 157 |
4 | 130–156 |
3 | 111–129 |
2 | 96–110 |
1 | 74–95 |
Tropical storm | 39–73 |
Tropical depression | $ \leq $ 38 |
Here's some sample desired behavior of the saffir-simpson
function from the CPO
interactions pane:
››› saffir-simpson(65) "Tropical storm" ››› saffir-simpson(111) "Category 3" ››› saffir-simpson(0) "Tropical depression"
Write tests for your function that address each category.
For your weather app, you wish to get a severe weather notification if the wind speed is classified as a hurricane (Category 1-5) and if the classification is for Vassar.
Write another function, severe-alert(wind-speed :: Number, latitude, ::
Number, longitude :: Number) → Boolean
, that will take 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. The weather alert would be over Vassar if the latitude is
between 41.656 and 41.693 and the longitude is between -73.908 and -73.880.
What if you wanted to only get an alert if the storm is the worst type (Category 5)
and over Vassar? Call this function
severe-alert2(wind-speed :: Number, latitude :: Number, longitude :: Number) → Boolean
.
It works the same way as the previous function, but only returns true
if the wind speed
was “Category 5”. How would this code be different from the previous version you wrote?
Can you think of a way you can use the saffir-simpson
function you have already written
to figure out the category of the hurricane, without having to duplicate some of the code you
wrote in saffir-simpson
?
lab02.arr
file to the Lab 2 assignment on Gradescope.This lab includes material adapted from Kathi Fisler and colleagues at Brown University.