Homework 4: How the Tables Have Turned!
Due: Tuesday October 1, 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
creating-functions-code.arr
disappearing-ghost-code.arr
jupiter-travel-board-code.arr
pikachus-getaway-code.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
Creating Functions
Below is a list of problems which you must come up a solution for. For each of those problems, you need to create a function, follow design and clarity guidelines, and thoroughly test. Here is an example:
- A function that takes in a number as an input and returns that number squared.
Example inputs -> outputs:
fun square(n :: Number) -> Number:
doc: "A functions that takes in an input number n,
and returns the square of that number."
n * n
end
Create a function (that works) for each of the following problem statements:
- A function that takes in a string input and a number input, and returns true if the number is smaller than or equal to the number of characters in that string, and false otherwise.
Example inputs -> outputs:
"Hello" , 3 -> true
"Hello" , 5 -> true
"Hello" , 8 -> false
- A function that takes in a string input and returns the same string lowercased, and without any whitespaces, periods, or quotation marks in the string.
Example inputs -> outputs:
" Hel l o " -> "hello"
" Hello.." -> "hello"
""Hello"" -> "hello"
- A function that takes in two string inputs and returns a number representing the difference in their lengths (how many characters the first string is longer than the second string–a negative number means that the second string is longer than the first).
Example inputs -> outputs:
"Hello" , "Hi" -> 3
"Hi" , "Hi" -> 0
"Hi" , "Hello" -> -3
- A function that takes in two number inputs and returns a boolean: true if the first number is the square root of the second number, and false otherwise.
Example inputs -> outputs:
7 , 49 -> true
4 , 16 -> true
2 , 8 -> false
- A function that takes in three string inputs and returns a number representing the length of the longest string.
Example inputs -> outputs:
"hi", "hello", "salutations" -> 11
"cs", "compsci", "computer science" -> 16
"cat", "dog", "sun" -> 3
The Ghost Disappears
After you helped them calculate the cost of using the ghost detector, Velma and Detective Pikachu decided it was too expensive, but they still brought you back with them to inspect the mansion.
While you’re inspecting the mansion, you come across a ghost! But since you don’t have the ghost detector data to prove it, your friends at the detective agency do not believe you. You have decided to recreate the scene so they can see what you saw. Your boss has recommended you use reactors
.
Here is an example of what you need to create.
The instructions are as follows:
- You must use the functions provided in the stencil code (meaning you’ll have to fill them out)
- You must use the variables provided
- You will be allowed to design the ghost’s face as you’d like, but:
- The ghost must have a half-oval body that goes from white to black
- The ghost must change colors 2 times (so use 3 different colors) i.e.: white → gray → black
- The color should change once when the ghost arrives at the 60% mark, and then change again when it is at the 95% mark.
- The ghost must have some facial feature, whether that be a red star, or black eyes (like in our example)
- When the ghost’s position is between 0 and 60% (≥ 0, <60) of the height of the image (HEIGHT), the ghost should move down 2 units per tick. When the ghost’s position is between 60 and 95 (≥ 60, <95) of HEIGHT, the ghost should move down 1 unit per tick. When the ghost’s position is more than 95% (≥95) of HEIGHT, the ghost should not move at all.
Look at the stencil code to get started. Don’t forget to follow the Design and Clarity guidelines!
Note: Remember that the coordinate system used by Pyret starts at the upper left, so as the ghost “moves”, its position will actually be increasing.
Remember that you can run the file to see your animation!
The Jupiter Travel Board
The table documentation will be very helpful for all of the problems in this assignment. In particular, you may want to take a look at filter-by, sort-by, build-column, and row-n.
After seeing the ghost, your crew was terrified and tried to get out of there as fast as possible! In your hurry, you put the wrong coordinates into the Mystery Machine and ended up in space!
The Mystery Machine is now quickly approaching Jupiter, but since it’s a gaseous planet, you can’t land. You try to turn the ship around but - oh no! Jupiter’s pull is too strong, and you’re getting sucked in! Fortunately, the aliens Maia and Jennifer appear and send you a message. They say they will get you out of there if you can help them with their work for the Jupiter Travel Board. Lucky you!
The Jupiter Travel Board is worried that no one can come to their planet, and is considering opening a Jupiter-themed resort on another planet. They need you to help choose the best planets for both Earthlings and Jupiterians. They know the following specifications:
- Jupiterians are warm-colored (red, orange, or yellow) because they are warm-hearted.
- Earthlings require a radius less than 10,000 km because otherwise gravity will crush them
- Earthlings require a distance from the sun between 100 and 1,000 million km (inclusive) or else they will freeze or burn
Using the data in the table, write two functions called jupiterian-planet
and earthling-planet
that take a row from the planet-data table as input and return a boolean, indicating whether or not the row represents a planet suited to jupertians / earthlings, based on the above requirements.
The Jupiter Travel Board wants to see on which planets both Earthlings and Jupiterians can coexist. Write a function called good-planets
that takes a Table as input and returns a Table with the rows for those planets that Jupiterians and Earthlings can both visit.
Make sure to include tests on planet-data
for each of these three functions. You can use T.row-n(N) for some table T and row-number N to extract a particular row for testing.
Testing
There are many ways to test tables. One way is to create a sample table to use for testing functions that return tables. For example:
example-table = table: detective-name :: String,
likes-mysteries :: Boolean, detective-level :: Number
row: "Detective Pikachu", true, 100
row: "Velma", true, 50
row: "Scooby Doo", false, 80
row: "Dr. Phil", true, 10
end
expected = table: detective-name :: String,
likes-mysteries :: Boolean, detective-level :: Number
row: "Dr. Phil", true, 10
row: "Velma", true, 50
row: "Scooby Doo", false, 80
row: "Detective Pikachu", true, 100
end
fun sample-function-sort-ascending(table :: Table) -> Table:
doc: "Sort table by level in increasing order"
sort-by(table, "detective-level", true)
end
check:
sample-function-sort-ascending(example-table) is expected
end
Note, this may be a bit painful for the planet data, given the nature of the numeric data (that you’d otherwise have to copy by hand). As an alternative, you can write an expression on the right side of the is that builds the expected output table from rows of your existing table (this is mostly useful for testing filter-by on small tables).
Here’s an example of how to create a table with two specific rows from the gradebook example:
add-row(
add-row(
gradebook.empty(), gradebook.row-n(1)),
gradebook.row-n(3))
gradebook.empty()
returns a table with the columns of gradebook, but no rows. The two calls to add-row insert rows from the gradebook table into the empty table (thus building up the table with rows 2 and 4 from the gradebook). You could put this into a where test as follows:
where:
<some expression> is add-row(add-row ...)
Detective Pikachu’s getaway
Detective Pikachu is taking a break from mystery-solving for a year to travel the universe and discover herself. She wants to find the planet with the longest number of days per year so she can have the longest vacation. Help Detective Pikachu out by creating a function called longest-year
that takes a Table as input and returns the name of the planet with the most number of days in its year.
-
Before you write any code: write out the steps/tasks that your function will need to perform, and indicate which seem like good candidates for helper functions. Write these in your README. Don’t edit this after you go on or start writing code – we want to see how you envisioned the steps before you started writing the actual code (you will be graded on just participation! It will not matter whether the plan is right – we just want to see how you’re doing with planning so we can adjust future assignments as needed).
-
Implement the function. You can use our proposed plan or your own, but you must include a step that adds a ‘days-per-year’ column to the table with the number of days in the year.
You can compute the number of days by dividing the orbital path length by the orbital speed:
days per year=2𝜋⋅1,000,000⋅distance from sunorbital speed
Note the 1,000,000 in the formula: this is necessary because the units on distance from the sun in the planet-data table are in millions of kilometers.
README.txt
Just include your planning process from the Detective Pikachu’s Getaway problem! :)
Brown University CSCI 0111 (Fall 2019)
Do you have feedback? Fill out this form.
Homework 4: How the Tables Have Turned!
Due: Tuesday October 1, 2019 at 9:00PM EST.
Handin
After completing the homework, you will submit:
README.txt
creating-functions-code.arr
disappearing-ghost-code.arr
jupiter-travel-board-code.arr
pikachus-getaway-code.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
Creating Functions
Below is a list of problems which you must come up a solution for. For each of those problems, you need to create a function, follow design and clarity guidelines, and thoroughly test. Here is an example:
Example inputs -> outputs:
Create a function (that works) for each of the following problem statements:
Example inputs -> outputs:
Example inputs -> outputs:
Example inputs -> outputs:
Example inputs -> outputs:
Example inputs -> outputs:
The Ghost Disappears
After you helped them calculate the cost of using the ghost detector, Velma and Detective Pikachu decided it was too expensive, but they still brought you back with them to inspect the mansion.
While you’re inspecting the mansion, you come across a ghost! But since you don’t have the ghost detector data to prove it, your friends at the detective agency do not believe you. You have decided to recreate the scene so they can see what you saw. Your boss has recommended you use
reactors
.Here is an example of what you need to create.
The instructions are as follows:
Look at the stencil code to get started. Don’t forget to follow the Design and Clarity guidelines!
Note: Remember that the coordinate system used by Pyret starts at the upper left, so as the ghost “moves”, its position will actually be increasing.
Remember that you can run the file to see your animation!
The Jupiter Travel Board
After seeing the ghost, your crew was terrified and tried to get out of there as fast as possible! In your hurry, you put the wrong coordinates into the Mystery Machine and ended up in space!
The Mystery Machine is now quickly approaching Jupiter, but since it’s a gaseous planet, you can’t land. You try to turn the ship around but - oh no! Jupiter’s pull is too strong, and you’re getting sucked in! Fortunately, the aliens Maia and Jennifer appear and send you a message. They say they will get you out of there if you can help them with their work for the Jupiter Travel Board. Lucky you!
The Jupiter Travel Board is worried that no one can come to their planet, and is considering opening a Jupiter-themed resort on another planet. They need you to help choose the best planets for both Earthlings and Jupiterians. They know the following specifications:
Using the data in the table, write two functions called
jupiterian-planet
andearthling-planet
that take a row from the planet-data table as input and return a boolean, indicating whether or not the row represents a planet suited to jupertians / earthlings, based on the above requirements.The Jupiter Travel Board wants to see on which planets both Earthlings and Jupiterians can coexist. Write a function called
good-planets
that takes a Table as input and returns a Table with the rows for those planets that Jupiterians and Earthlings can both visit.Make sure to include tests on
planet-data
for each of these three functions. You can use T.row-n(N) for some table T and row-number N to extract a particular row for testing.Testing
There are many ways to test tables. One way is to create a sample table to use for testing functions that return tables. For example:
Note, this may be a bit painful for the planet data, given the nature of the numeric data (that you’d otherwise have to copy by hand). As an alternative, you can write an expression on the right side of the is that builds the expected output table from rows of your existing table (this is mostly useful for testing filter-by on small tables).
Here’s an example of how to create a table with two specific rows from the gradebook example:
gradebook.empty()
returns a table with the columns of gradebook, but no rows. The two calls to add-row insert rows from the gradebook table into the empty table (thus building up the table with rows 2 and 4 from the gradebook). You could put this into a where test as follows:Detective Pikachu’s getaway
Detective Pikachu is taking a break from mystery-solving for a year to travel the universe and discover herself. She wants to find the planet with the longest number of days per year so she can have the longest vacation. Help Detective Pikachu out by creating a function called
longest-year
that takes a Table as input and returns the name of the planet with the most number of days in its year.Before you write any code: write out the steps/tasks that your function will need to perform, and indicate which seem like good candidates for helper functions. Write these in your README. Don’t edit this after you go on or start writing code – we want to see how you envisioned the steps before you started writing the actual code (you will be graded on just participation! It will not matter whether the plan is right – we just want to see how you’re doing with planning so we can adjust future assignments as needed).
Implement the function. You can use our proposed plan or your own, but you must include a step that adds a ‘days-per-year’ column to the table with the number of days in the year.
days per year=2𝜋⋅1,000,000⋅distance from sunorbital speed
You can compute the number of days by dividing the orbital path length by the orbital speed:
Note the 1,000,000 in the formula: this is necessary because the units on distance from the sun in the planet-data table are in millions of kilometers.
README.txt
Just include your planning process from the Detective Pikachu’s Getaway problem! :)