After completing the homework, you will submit:
safety.arr
calculate-rate.arr
sunset.arr
Hand in your work at this Google Form (https://goo.gl/forms/Fkr461qrsm27iZnP2).
Is it safe? After landing on Venus, you have to make sure that the surface conditions are safe for you to leave the spaceship. There are two main things you have to check: atmosphere and weather.
Write a function 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 “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"
!
Copy this header for is-planet-safe
into a file called safety.arr
, and then complete the function as described:
fun is-planet-safe(atmosphere :: String, temp :: Number, pressure :: Number) -> String:
...
end
Hints:
Heila likes to call Eli on Mars, but unfortunately Verizon’s coverage in space is pretty expensive. She wrote some code to help her calculate the rates for each call; help her clean up her code!
Heila wrote the function calculate-rate, which takes in the number of minutes that she’ll spend on the call and returns the cost of the call.
fun calculate-rate(minutes):
low-minutes-cost = 5
medium-minutes-cost = 10
high-minutes-cost = 15
if ((minutes >= 0) and (minutes < 10)):
(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
Help Heila clean up her code by making the following improvements:
It’s been great hanging out on Venus with Heila, buts it’s time to move on. Before you leave, you want to experience a beautiful sunset with Heila. Unfortunately, days on Venus last 243 Earth days, so you would have to hang out for a long time! Instead, you will code a simulated sunset!
Copy the sample code below into a new pyret file called sunset.arr to get started.
HEIGHT = 250
WIDTH = 400
RADIUS = HEIGHT * 0.1
bg-url = "https://cs.brown.edu/courses/csci0111/data/hw3-sunset-bg.png"
SKY = scale(5/7, image-url(bg-url))
fun drop-sun(sun-pos :: Number):
...
end
fun sun-color(sun-pos :: Number) -> String:
...
end
fun draw-sun(sun-pos :: Number) -> Image:
place-image(circle(RADIUS, "solid", sun-color(sun-pos)), WIDTH / 2, sun-pos, SKY)
end
sunset = reactor:
init: 0,
on-tick: drop-sun,
to-draw: draw-sun
end
interact(sunset)
Draw-sun should draw the sun, but the colors should change to reflect the sun’s setting. The sun should set by continually increasing its position in drop-sun, but this should also vary based on the location of the sun. Specifically, the sun should be a circle with a radius of RADIUS. When the sun’s position is between 0 and 60% (, ) of the height of the sky (HEIGHT), the sun should move down 2 per tick and should be drawn in yellow. When the sun’s position is between 60 and 95% (, ) of HEIGHT, the sun should move down 1 per tick and should be drawn in orange. When the sun’s position is more than 95% () of HEIGHT, the sun should not move at all and should be drawn in red.
Clarification (added Friday Sep 21): Whether or not the boundaries of the height percentages are inclusive or exclusive has been added.
Remember that the coordinate system used by Pyret starts at the upper left, so as the sun “sets”, its position will actually be increasing.
Remember that you can run the file to see your animation!