Due: Monday, July 1 at 9am (submit through this Google form – directions are at the bottom of the page)
To make sure you can write Pyret functions.
To make sure you can test Pyret functions.
To make sure you can write conditional expressions.
To make sure you can create helper functions and constant definitions to clean up code.
To give you practice working with Pyret’s documentation.
Collaboration Policy: Collaborating with others on this assignment is fine. However, we strongly recommend that you type these out on your own and try doing at least a couple of them on your own. This assignment is building up core skills that you’ll need throughout the course.
Note: Apologies for the weird color-striping on the code snippets. I’m not sure why that is happening, but am working on it.
1 Calling Plans
A co-worker has written the following function to compute the cost of an out-of-network mobile phone call.
fun calculate-rate(minutes): |
low-minutes-cost = 5 |
medium-minutes-cost = 10 |
high-minutes-cost = 15 |
if ((minutes >= 0) and (minutes < 11)): |
(minutes / (low-minutes-cost / 12)) * (10 + (minutes * 3)) |
else if ((minutes >= 11) and (minutes < 20)): |
(minutes / (medium-minutes-cost / 12)) * (10 + (minutes * 3)) |
else if (minutes >= 20) : |
(minutes / (high-minutes-cost / 12)) * (10 + (minutes * 3)) |
end |
where: |
calculate-rate(12) is 662.4 |
end |
Your job is to clean up this code in the following ways:
Add the type to the input variable and the return type of the function
Add a doc string
Write more thorough test cases, and
Clean up the code to avoid needless repetition. You may introduce helper functions or constants as you see fit.
Underneath your code, include a prose description of how you went about this problem. Write your description as a multi-line comment within your Pyret file. A multi-line comment looks as follows:
#| |
this is a comment |
that spans multiple lines. |
The hashtag/vertical bar pattern |
tells Pyret this is a multi-line comment. |
|# |
2 Astronaut Safety
You are an astronaut who has just landed on an unknown planet. You have to make sure that the conditions outside are safe for you to leave the spacecraft. There are two main things you have to check: atmosphere and weather.
Write a function named is-planet-safe, which takes in a String representing the gas in the atmosphere, a Number representing the current temperature, and a Number representing the pressure on the planet, and returns the String “safe” if it is safe to leave the spaceship, "warning" if we should be careful, and “dangerous!” if not.
It is safe to leave the spaceship if all of the following are true:
The gas in the atmosphere is “oxygen” or “hydrogen”
The temperature on the planet is between 20 and 110 degrees
The pressure on the planet is between 0.5 and 3 atmospheres
We have special space suits that enable us to tolerate extreme environments, so if only one of these conditions is false, the function should return "warning": we can leave the ship, but we need to watch out. If more than one condition is false, we need to stay inside, and the function should return "dangerous"!
The following header sets up the function name, variable names, and types. Copy this into your file, then fill in the function.
fun is-planet-safe(atmosphere :: String, |
temp :: Number, |
pressure :: Number) -> String: |
<fill in here> |
end |
You will probably want an if expression to decide which String to return. Generally, if you only need to return a boolean you can use ands and ors, but when you need to return something else you will need if.
3 Malaria Risk
You’re working for a public health organization that has been studying malaria prevention in a rural area. Your team has collected data on which people have been hit with malaria cases, and discovered that people who have mosquito nets and either live 1000 feet from the lake or have had maleria at least 3 times are at low risk for getting sick.
Write a function named is-low-risk that takes THREE inputs about a person: the distance (in feet) from the lake to their house, whether they have mosquito nets, and how many times they have had malaria before. The function should return a boolean indicating whether they are low risk for getting malaria.
Remember to write a good set of where: examples for each function that you write. The quality of your tests is something we will look at carefully in grading throughout this course. This assignment is more for learning how to test, with the grading on tests getting more detailed after this assignment.
If you think you need an operator or function beyond what you remember from class, go to the Pyret documentation. Each type of data that we cover has a section under the "Builtins and Libraries" link on the left sidebar. After you click on your type of data, the lower part of that left sidebar will list all the functions/operations available for that type of data.
We will be looking for whether your code:
Has types and docstrings
Uses spacing and line-breaks appropriately to make your code readable
Uses descriptive variable names
Has a set of where examples that cover most input scenarios that lead to different answers from a function
Produces the expected answer
Make sure you have included a collaboration statement as a comment at the top of your file. This statement should look something like:
# I have done this entire assignment on my own
or
# For the astronaut problem, Jaime and I developed the code together
When you are done, download your file from Pyret (under the "File" menu), save it with the name hwk2.arr, and upload your file to this Google form
Here’s an animated-gif showing the steps in case you need them.