Homework 3: The Haunted House
Due: Tuesday September 24, 2019 at 9:00PM EST.
Handin
- Download your file(s) and make sure the follow the naming convention listed in the assignment handout. Hand in your work on Gradescope. Look at this guide for directions.
- You may submit as many times as you want. Only your latest submission will be graded. This means that if you submit after the deadline, you will be using a late day – so do NOT submit after the deadline unless you plan on using late days.
- Do not put your name anywhere in any of the handin files. Doing so will result in a deduction.
- The README template can be found here
- Don’t forget to follow the design and clarity guide since you will be doing most of the coding in this assignment!
After completing the homework, you will submit:
-
README.txt
-
coordinates.arr
-
haunted-house.arr
-
ghost-detector.arr
If you are using late days, make sure to make a note of that in your readme. Remember, you may only use a maximum of 3 late days per assignment. If the assignment is late (and you do NOT have anymore late days) no credit will be given.
Helpful Things
Documentation
- Pyret Documentation, accessible from the Pirate button in the top left corner of code.pyret.org. The Image and String documentation will likely be the most useful for this assignment.
- Design and Clarity Guide I highly encourage you to read over this to remind you about good coding practices! This will be a very useful document for this assignment.
Useful Functions
-
not
is a builtin function that takes a Boolean, and inverts it: not(true)
→ false
and not(false)
→ true
-
==
is an equality test; 3 == 3
→ true
and 3 == 4
→ false
.
-
<>
is an inequality test; 3 <> 4
→ true
and 3 <> 3
→ false
-
if
indicates a conditional and is used when there are two possible outcomes for an input, depending on a condition. Conditions should always return true or false. Example:
inputone = 1
if (inputone == 1):
"Outcome one: The input was one"
else:
"Outcome two: The input was not one"
end
-
“inclusive” means including - for example, suppose I wanted represent the numbers 1, 2, 3, 4, 5. You could say, 1 through 5 inclusive, meaning all the numbers between 1 and 5, including 1 and 5 themselves.
The Assignment
Coordinates
Velma and Detective Pikachu will need to investigate a haunted house as a part of the case they are trying to solve. In order to get to the haunted house, they need to use the Mystery Mobile! To navigate the Mystery Mobile, you need to enter its coordinates. However, if you don’t follow the Mystery Mobile’s rules about how to enter the coordinates, you will be unable to get to the haunted house!
The coordinate string must:
-
Be wrapped in diamond parentheses ( "<"
and ">"
are the first and last characters, respectively)
- Things to think about: For this part, think about what we are looking for. what parts of the string matter? Answer: Just the first and last characters. What string functions let you check that?
-
Have a comma as its middle character, meaning there must be the same number of characters on either side of it (i.e. "<db,in>"
and "<slow,down>"
are ok, but "<d,abc>"
and "<ddab>"
are not)
- Things to think about: What does this tell us about the length of the string? Can it be even?
- Things to think about: How can we check that the middle character is a comma? Start by finding the index of the middle character. It may be helpful to consider some examples: for instance, in the string
"<a,b>"
the middle character’s index is 2
. What’s the index of the middle character of "<dog,cat>"
? Once you’ve determined the index of the middle character, you’ll need to make sure that the character at that index is a comma.
-
Not contain a minus sign (-
)
-
Not contain the sequence err
(i.e. "<er,rr>"
and "<0e,rr>"
is ok, but "<err,1234>"
and "<12340,1err0"
is not)
- Things to think about: For the last two, the key word is contain. Is there a string function that looks for specific character sequences?
Fill in the function coordinate-check
in the file coordinates.arr
that takes in a coordinate String and returns true
if the coordinates follow the rules, and false
if they do not.
Examples:
-
coordinate-check("<abc,-12>")
→ false
(contains -
)
-
coordinate-check("<dog,cat>")
→ true
In order to check if your function works or not, we encourage you to write tests. The tests provided are only a small subset of the full test suite. You should not assume your function works because is passes those test cases, as there are other cases that may have not been tested. Testing will assert that your function is returning the output you expect. These are examples of a test case:
coordinate-check("<abc,-12>") is false
coordinate-check("<dog,cat>") is true
Finally, format the function in such a way that follows the Design and Clarity Guide.
Hint: Take a look at this documentation and this documentation as you may find it useful.
Warning: Being able to look in language documentation for useful operations is an important skill, which is why we aren’t telling you exactly which string operations to use. We want you to look at the string documentation to find useful operations for solving this problem. However, limit yourself to operations with input and output types that we have used this semester (Number, String, Boolean). Don’t use operations that return List, as we haven’t covered that yet.
Inspecting the House
Is it safe? After arriving at the haunted house, you have to make sure that the conditions of the house are safe for you to enter and examine it.
Fill in the function called is-house-safe
, which takes in:
monster-present
: a String representing the monters that are present
num-rodents
: a Number from 0 to 1000 representing the number of rodents that are currently in the house
chance-ghosts
: a Number from 0 to 1, representing the likelihood of ghosts (The scale starts at 0, meaning they are definitely not present and goes up to 1, meaning they are definitely present)
The function should return “safe”
if it is safe to enter the house to examine it, "warning"
if we should be careful, and “dangerous!”
if we should not enter.
It is safe to enter the haunted house if all of the following are true:
-
The monster that is in the house is “none”, “sully” or “frankenstein”.
-
The number of rodents in the house is between 10 and 47, inclusive (anything less than 10 means there’s something scaring them away, which is not good)
-
The likelihood of ghosts in the house is between 0 and 0.25, inclusive
If only one of these conditions is false, the function should return "warning"
: meaning we can enter the house, but need to be cautious while examining the house. If more than one of the conditions is false, then we cannot enter, the risk is too high! Thus, the function should return "dangerous"
!
Don’t forget to follow the design and clarity guidelines!
Hint: You will probably want to use an if expression to decide which String to return! Generally, if you only need to return a Boolean, you can use ands and ors to express your conditionals, but when you need to return something else you will need an if statement.
Ghost Detector
Before Velma and Detective Pikachu use the ghost detector tool (it’s very expensive to use), they asked Jennifer if she could lend them her price calculator for the ghost detector. Jennifer wrote some code to help her calculate the rates for each use, but unfortunately she has forgotten to follow the design and clarity guide! In order for Velma and Detective Pikachu to understand Jennifer’s code, you’ll need to help by cleaning up the code!
Jennifer wrote the function calculate-rate, which takes in the number of minutes that they use the ghost detector and returns the cost of the use.
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
Help Jennifer clean up her code by making the following improvements:
- Add type annotations,
- Add docstrings,
- Write more thorough test cases, and
- Clean up the code as you see fit. In particular, there are some instances of repetition!
README.txt
No additional responses required for this week! Still submit a README though (we use it to track late days and collaboration stuff)!
Brown University CSCI 0111 (Fall 2019)
Do you have feedback? Fill out this form.
Homework 3: The Haunted House
Due: Tuesday September 24, 2019 at 9:00PM EST.
Handin
After completing the homework, you will submit:
README.txt
coordinates.arr
haunted-house.arr
ghost-detector.arr
If you are using late days, make sure to make a note of that in your readme. Remember, you may only use a maximum of 3 late days per assignment. If the assignment is late (and you do NOT have anymore late days) no credit will be given.
Helpful Things
Documentation
Useful Functions
not
is a builtin function that takes a Boolean, and inverts it:not(true)
→false
andnot(false)
→true
==
is an equality test;3 == 3
→true
and3 == 4
→false
.<>
is an inequality test;3 <> 4
→true
and3 <> 3
→false
if
indicates a conditional and is used when there are two possible outcomes for an input, depending on a condition. Conditions should always return true or false. Example:“inclusive” means including - for example, suppose I wanted represent the numbers 1, 2, 3, 4, 5. You could say, 1 through 5 inclusive, meaning all the numbers between 1 and 5, including 1 and 5 themselves.
The Assignment
Coordinates
Velma and Detective Pikachu will need to investigate a haunted house as a part of the case they are trying to solve. In order to get to the haunted house, they need to use the Mystery Mobile! To navigate the Mystery Mobile, you need to enter its coordinates. However, if you don’t follow the Mystery Mobile’s rules about how to enter the coordinates, you will be unable to get to the haunted house!
The coordinate string must:
Be wrapped in diamond parentheses (
"<"
and">"
are the first and last characters, respectively)Have a comma as its middle character, meaning there must be the same number of characters on either side of it (i.e.
"<db,in>"
and"<slow,down>"
are ok, but"<d,abc>"
and "<ddab>"
are not)"<a,b>"
the middle character’s index is2
. What’s the index of the middle character of"<dog,cat>"
? Once you’ve determined the index of the middle character, you’ll need to make sure that the character at that index is a comma.Not contain a minus sign (
-
)Not contain the sequence
err
(i.e."<er,rr>"
and"<0e,rr>"
is ok, but"<err,1234>"
and"<12340,1err0"
is not)Fill in the function
coordinate-check
in the filecoordinates.arr
that takes in a coordinate String and returnstrue
if the coordinates follow the rules, andfalse
if they do not.Examples:
coordinate-check("<abc,-12>")
→false
(contains
-
)coordinate-check("<dog,cat>")
→true
In order to check if your function works or not, we encourage you to write tests. The tests provided are only a small subset of the full test suite. You should not assume your function works because is passes those test cases, as there are other cases that may have not been tested. Testing will assert that your function is returning the output you expect. These are examples of a test case:
Finally, format the function in such a way that follows the Design and Clarity Guide.
Hint: Take a look at this documentation and this documentation as you may find it useful.
Warning: Being able to look in language documentation for useful operations is an important skill, which is why we aren’t telling you exactly which string operations to use. We want you to look at the string documentation to find useful operations for solving this problem. However, limit yourself to operations with input and output types that we have used this semester (Number, String, Boolean). Don’t use operations that return List, as we haven’t covered that yet.
Inspecting the House
Is it safe? After arriving at the haunted house, you have to make sure that the conditions of the house are safe for you to enter and examine it.
Fill in the function called
is-house-safe
, which takes in:monster-present
: a String representing the monters that are presentnum-rodents
: a Number from 0 to 1000 representing the number of rodents that are currently in the housechance-ghosts
: a Number from 0 to 1, representing the likelihood of ghosts (The scale starts at 0, meaning they are definitely not present and goes up to 1, meaning they are definitely present)The function should return
“safe”
if it is safe to enter the house to examine it,"warning"
if we should be careful, and“dangerous!”
if we should not enter.It is safe to enter the haunted house if all of the following are true:
The monster that is in the house is “none”, “sully” or “frankenstein”.
The number of rodents in the house is between 10 and 47, inclusive (anything less than 10 means there’s something scaring them away, which is not good)
The likelihood of ghosts in the house is between 0 and 0.25, inclusive
If only one of these conditions is false, the function should return
"warning"
: meaning we can enter the house, but need to be cautious while examining the house. If more than one of the conditions is false, then we cannot enter, the risk is too high! Thus, the function should return"dangerous"
!Don’t forget to follow the design and clarity guidelines!
Hint: You will probably want to use an if expression to decide which String to return! Generally, if you only need to return a Boolean, you can use ands and ors to express your conditionals, but when you need to return something else you will need an if statement.
Ghost Detector
Before Velma and Detective Pikachu use the ghost detector tool (it’s very expensive to use), they asked Jennifer if she could lend them her price calculator for the ghost detector. Jennifer wrote some code to help her calculate the rates for each use, but unfortunately she has forgotten to follow the design and clarity guide! In order for Velma and Detective Pikachu to understand Jennifer’s code, you’ll need to help by cleaning up the code!
Jennifer wrote the function calculate-rate, which takes in the number of minutes that they use the ghost detector and returns the cost of the use.
Help Jennifer clean up her code by making the following improvements:
README.txt
No additional responses required for this week! Still submit a README though (we use it to track late days and collaboration stuff)!