Lab 2: Conditionals and Reading Code

For this lab, we suggest that you start a shared Google Doc with your partner for the written exercises and a Pyret file for writing code.

Problem 1: Greetings

You have joined a club at Brown that tries to make useful mobile apps for students. As a starter project, they are making a simple app that displays a greeting on your phone based on the time of day.

The current time is something your phone stores automatically. We can pretend that this is just a fixed value (that gets read whenever you turn on your phone). The time is expressed in "military time," such that 9:00AM is 0900 and 6:00PM is 1800.

Write an if expression that produces a greeting based on a constant TIME. If TIME is between 5:00AM and 12:00PM, we want the if expression to output "Good Morning". If TIME is past 12:00PM but before 6:00PM, we want the if expression to output "Good Afternoon". Otherwise, we want the if expression to output "Good Night".

NOTE: if expressions don't have to occur within functions. You only need a function if the computation depends on an input that is provided when the function is called. For example, this is perfectly valid code:

number = 111

if (number == 111):
    "Yay"
end

Problem 1.1: Order of Clauses

Some members of the club came up with their own implementation of the app, but they are having some issues with its output. Consider their code:

TIME = 0900

if (TIME >= 0500):
  "Good Morning"
else if (TIME >= 1200) and (TIME <= 1800):
  "Good Afternoon"
else:
  "Good Night"
end

Can you find an example where the code does not return what you think it should return? Discuss with your partner about how the order of if expression clauses can affect the behavior of code. Write your thoughts in your Google Doc.

Problem 1.2: Nested Conditionals

Another member of the club came up with an implementation of the app, but they chose to use nested conditionals, which are if expressions contained within other if expressions:

if (TIME <= 1200):
    if (TIME >= 0500):
        "Good morning"
    else:
        "Good night"
    end
else:
    if (TIME <= 1800):
        "Good afternoon"
    else:
        "Good night"
    end
end

Compare and contrast your code with the above example. What are possible drawbacks to using nested conditionals? Write your answers in your Google Doc.

Problem 1.3: not

You want to decide whether the app should use a light or dark background depending on the time of day. Someone in the club writes the following two if-else expressions:

if (not((TIME > 0600) and (TIME < 1800))):
    "Dark"
else:
    "Light"
end

How can you simplifiy the if expression above by eliminating the not function? Copy the code into your Pyret file and modify it to have the same output, but without using not.


CHECKPOINT:

Call a TA over when you’ve finished. Be ready to explain your written answers as well as your code!


Problem 2: Weather Alerts - Saffir-Simpson

Now, your club is making a simple weather app that alerts users of severe weather based on wind speed. Because the members of the club all live in Providence, Rhode Island, they decide to use 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.

>>> saffir-simpson(65)
"Tropical Storm"
>>> saffir-simpson(111)
"Category 3"
>>> saffir-simpson(0)
"Tropical Depression"

Write tests for your function that address each category.

Problem 2.1: Weather Alerts - Tropical Cyclone Scales

Students across campus install the app. Adeola, a new international student from Nigeria, asks you what "Tropical Depression" means, as that isn't terminology she's heard before. It turns out that meterologists around the world have different terms and different formulas for determining storm strength! A storm over the Atlantic Ocean (e.g. USA), might be called "Category 3." A storm over the south-west Indian Ocean (e.g. Africa) might be called a "Tropical Cyclone." A similar storm over the North Indian Ocean (e.g. India) might be described as a "Severe Cyclonic Storm."

To complete the following problems, you will need to use data listed in the tables in the Wikipedia article above. Please ignore the table at the very bottom of the page and instead use the data listed in the tables in the region-specific sections.

Here are the regions the Wikipedia article classifies.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Though each region classifies "sustained winds" as a different duration of time, we will not worry about that for our function. Assume that the given inputs meet the sutained wind requirements for each region.

We've included the approximate latitude and longitude ranges for each of the regions, as well as the sustained wind duration in each region. Latitude means North or South of the Equator, where a positive value is North and a negative value is South. Longitude means East or West of the Prime Meridian, where a positive value is East and a negative value is West, and 180° is equal to -180°. International meteorology is complicated!

| Region | Latitude Range | Longitude Range |
| | | | | |
| Atlantic, Eastern and Central Pacific | 0° to 90° | -180° to 44° |1 minute|
| Western Pacific | 0° to 90° | 100° to 180° | 10 minutes |
| North Indian Ocean | 0° to 90° | 45° to 99° |3 minutes
| South-West Indian Ocean | -90° to -1° | 45° to 90° | 0 minutes
| Australia and Fiji | -90° to -1° | 91° to -120° |10 minutes

Write another function storm-type(speed :: Number, longitude :: Number, latitude :: Number) -> String that takes in speed (in km/h), longitude, and latitude as arguments and returns the storm type of that region.

Write tests to make sure your function is behaving how you expect it to.

HINT: Think about using helper functions to make your code simpler and easier to organize.

Problem 2.3 - Severe Weather Alerts

A student has submitted feedback on your app, saying that she'd like to get the severe weather notification only when it's the worst classification in her area. For example, in the Western Pacific, the worst classification would be a Typhoon, and in the North Indian Ocean, it would be a Super Cyclonic Storm.

Write another function severe-alert(wind-speed :: Number, longitude :: Number, latitude :: Number) -> Boolean that will take the current wind speeds in their area (in km/hr) and the location of the user (latitude & longitude), and returns true if the storm is the worst type and false if otherwise.

Practice writing this function using nested if expressions, and then come up with another version (servere-alert2) that uses just Boolean operations (and, or, not, ==, >, <, etc.). How do these versions compare to your versions of saffir-simpson and storm-type? Are there any if expressions that could be eliminated in those functions?


CHECKPOINT:

Call a TA over when you’ve finished Problem 2, 2.1, 2.2, and 2.3.


Problem 3: Conditional Graphics

Brown's coding club decides to add an image to the storm warnings. It will depict a beach scene that reflects the category of weather conditions:

  • If the weather is good (Category 1), present a beach with "gold" sand, "aqua" water, "blue" sky, and a chair in upright position:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • If there is a lower-grade storm (Category 2), present a beach with "gold" sand, "aqua" water, "gray" sky and a chair tipped over:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • If there is a higher-grade storm (Category 3), present a beach with "gold" sand, "aqua" water, "black" sky and a chair upside down and airborne:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Discuss with your partner about how you would approach implementing these conditional images. You don't need to write any code just discuss design strategy and write your answers in your Google Doc.


CHECKPOINT:

Call a TA over when you've discussed, and they'll give you a link to the next part of this lab.