Homework 4: How the Tables Have Turned!

Due: Tuesday October 1, 2019 at 9:00PM EST.

enter image description here

Handin

After completing the homework, you will submit:

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

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:

    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:

Example inputs -> outputs:

    "Hello" , 3   -> true
    "Hello" , 5   -> true
    "Hello" , 8   -> false

Example inputs -> outputs:


    " Hel l o "   -> "hello"
    " Hello.."    -> "hello"
    ""Hello""     -> "hello"

Example inputs -> outputs:

    "Hello" , "Hi"    -> 3
    "Hi"    , "Hi"    -> 0
    "Hi"    , "Hello" -> -3

Example inputs -> outputs:

    7 , 49    -> true
    4 , 16    -> true
    2 , 8     -> false

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.
this example

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

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:

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.

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.