
Title:
Subway Planning Solution  Design of Computer Programs

Description:

Here is my solution.

For subway I decided I'm going to return something that looks

just like the successor function, only it's going to have all possible successors.

It's going to be the dictionary of this form for any station that you ask me for.

Stations are states in this problem.

All you need to represent a state in this problem is what station am I currently at.

Each of those I'm going to give you a dictionary that looks like a successor function.

That is it has a combination of stateaction pairs.

The state is just going to be the neighboring station.

Then the action is the line that you take.

For example, if I'm at Bowdoin, then one of the states I can arrive at is government.

The action I take to get there is follow the blue line.

How do I do that?

Well, one thing is I import the collections module.

I use collections.defaultdict.

I'm saying successors is a dictionary,

and by default if there's a value that isn't there, the default is going to be a dictionary.

Here's what successors look like, and the default value

if I ask for the value of a station and there's nothing there, make it be an empty dictionary.

The line items are a set of line stops pairs.

I'm going to go over thoseline, name, and stops.

Then I'm going to look at all the overlapping pairsBowdoingovernment,

governmentstate, stateaquariumthat I get by splitting up the stops into a list on spaces.

Then I'm saying a successor of going from a to b, from Bowdoin to government

you can do that along a linenameand similarly going from government to Bowdoin.

You can do that along the linename here, blue.

Then return the successors.

Overlapping_pairs takes any sequence and just gives me those pairs.

If you give me this sequence of this turned into a list,

the overlapping_pairs are first bowdoingovernment, then government and state,

then state and aquarium.

This is useful here, and it's useful in general.

Here's my ride function. It's now very easy.

It's the shortest path search. That makes sense.

I don't need lowest cost search, because there are no costs here.

I'm just going from one station to the next.

I start here. Where am I trying to get to?

Well, the goal is to get to there.

I'm trying to get to someplace whereand I just put the function inline here

rather than defining it separately.

You could have done it either way.

Because the functions were short, I decided to put them inline.

But naming the function would also be fine.

Lambda s"s" stands for state or for station or for stop, and we ask "are we there?"

Then the successor function.

Well, we've already built up the successor function in this subway systemBoston

so we just say looking into boston, that's a dictionary

and look for the station that I'm currently at, and that will give me a list of successors.

Then shortestpathsearch does the rest.

What does longest_ride do?

First, I have to find all the stops, and that's a little bit complicated,

because I've hidden them away in this dictionary for the system,

but I can go through with this generator expression to come up with a set of stops.

Then I just iterate through all possible stations a and b,

look for a in all possible stops and for b in all possible stops,

generate the ridethe shortestpathsearch between them,

and then take the longest out of all those shortest paths,

and that's my longest ride.