Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Metro

ReleasedDue
Thu, Oct 05Tue, Oct 10

Homework 5: Metro

In this homework, we will examine a dataset of Washington Metro stations. Our goal is to be able to answer some useful questions about Metro stations in Washington, like where they are located, and how to navigate between them.

To start, let’s import the dataset. Copy and paste the code snippet below into your Pyret editor:

include shared-gdrive("[hash here]")

This line links in a table on a Pyret source file stored on Google Drive. You can access that file here:  

Once you’ve imported the dataset, it will be stored as a pyret Table bound to the variable metro-table. Try taking a look at the table in your REPL to ensure you’ve imported sucessfully.

Task 1:

Let’s start by getting a list of the Metro stations in DC. Looking at the NAME column in the table, we can see some names are repeated. This indicates that a given Metro station has multiple entrances. However, different entrances to the same station may have different names. In that case, we consider the “root”, the shared words of both entrance names to be the name of the station. For example, “Cleveland Park” and “Cleveland Park Elevator” are two different entrance names for the same station, but we consider them both to be from the station “Cleveland Park”. Write a function get-stations that takes in a list of entrance names and returns a list of the corresponding stations (with no duplicates). Then, get a list of stations in DC as follows:

dc-stations = get-stations(metro-table.get_column("NAME"))

Creating a table of information

Our goal now is to create a table with information about each Metro station. The following 2 functions may be helpful:

  • create-table-with-col(colname :: String, colvals :: List<Any>) -> Table
    This function takes a column header (like "name") and a list of values and creates a single-column table with those values.
  • add-col :: (t :: Table, colname :: String, colvals :: List<Any>) -> Table
    This function takes a table and adds a new (rightmost) column with the given colname and values. The list of colvals must be the same length as the number of rows (without the header/column name.)

Task 2:

One thing we want to know is how many entrances each Metro station has. Create a column for your table that contains the number of entrances each Metro station has.

Note: You are not allowed to use the count function.

Task 3:

Now, we want to examine the streets that the entrances lie on. Extract the column EXIT_TO_ST from the table and call it exit-st. exit-st should be a list of strings, with each string representing the location of one entrance. Create a list of lists exit-st-words where each interior list contains the individual words of an entrance location in order.

Task 4:

We turn our attention to the Avenues. We want to obtain a list of the names of all the Avenues in station entrances. To start, create a list named avenue-list that contains only the entrances locations with AVENUE somewhere in their name.

Task 5:

Looking at some examples, we can see that the word that precedes ‘AVENUE’ often gives the name of that particular avenue. However, in some cases, the name of the avenue is multiple words long. In that case, we’ll assume the first word after OF or AND starts the name of the avenue. Write a function find-avenue-name that given a list of words, returns a list of the names of the avenues in that list.

find-avenue-name([list: "SOUTH", "SIDE", "OF", "RHODE", "ISLAND", "AVENUE", "AT", "RHODE", ISLAND", "PLACE", "SHOPPING", "CENTER"]) = [list: "RHODE ISLAND AVENUE"]

find-avenue-name([list: "NE", "CORNER", "OF", "GEORGIA", "AVENUE", NW", "AND", "NEW", "HAMPSHIRE", "AVENUE", "NW"]) = [list: "GEORGIA AVENUE", "NEW HAMPSHIRE AVENUE"]

find-avenue-name([list: "AND", "NORTH", "OF", "PENN", "AVENUE", "COW"]) = [list: "PENN AVENUE"]

Task 6:

Using the function you wrote above, create a list station-avenues that has a unique list of all of the avenues in DC that have a Metro entrance along it.

Task 7:

Putting the above parts together, write a function create-avenues-table that takes as input a table of subway station entrance names and locations, and returns a table with two columns, names of the avenue names, and count with the number of station entrances on the respective avenues.

#

Structured Data

We are now going to explore storing data about Metro stations in a custom data type.

Task 8:

Create a datatype MetroStation. Its constructor is metro-station and has 3 fields: station-name, station-lines, and entrances. station-name is the same name as would be given in Task 1. station-lines is a list of strings representing the lines the station is on. entrances is a list of the entrance locations (given by the EXIT_TO_ST columns) for the station.

Task 9:

Using the data from the table, create a list dc-stations of MetroStations that contains appropriate information (Each metro station in DC has a corresponding MetroStation).

Task 10:

Create a function find-transfer that takes as arguments two MetroStations and the list dc-stations. If either of the Metro Stations as identified by name do not exist in dc-stations, return "Station Not Found". If it is possible to travel between the stations without transfering, return "No Transfer Needed". Otherwise, return the list of stations you could possibly transfer at to travel between the stations, assuming you only want to transfer once. You can take a look at a map of the Washington Metro to convince yourself that this is always possible.

Task 11:

Suppose one entire line must be removed from service. Write a function find-removal, that given a list of MetroStations, returns a string representing the line that, when removed, results in the fewest number of inaccessible stations, i.e. stations that are no longer reachable given the new set of active lines.