Toll Lanes: Car Arrival Counting

The questions below are due on Monday July 26, 2021; 11:00:00 AM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
1

Description

You want to examine the behavior of cars passing through a toll road checkpoint. The toll checkpoint you're examining has n toll booths (one for each of the n lanes), each of which takes exactly 1 minute to toll a car2.

Using a camera mounted at the checkpoint and some image processing, you have compiled historical data about the checkpoint in a Python list lane_lines. Each element of lane_lines is itself a list, containing n nonnegative integers which indicate how many cars are in line for each of the n lane lines. There is one list in lane_lines for every minute of recorded data—the first element in lane_lines describes the length of the lane lines at the beginning of the first minute; the second element describes the length of the lines at the beginning of the second minute; etc.

For example,

lane_lines = [[0, 0, 0],
              [1, 1, 0],
              [0, 0, 2],
              [2, 3, 1]]

contains data on three toll lanes over three minutes. It says:

  • At the beginning of the first minute, there were no cars in any of the toll lanes.
  • At the beginning of the second minute, there was one car in line for the first lane and one in line for the second, and none in line for the third.
  • At the beginning of the third minute, there were no cars in line for the first or second lanes, and two cars in line for the third.
  • At the beginning of the fourth minute (i.e. the end of the third minute), there were two cars in line for the first lane, three cars in line for the second lane, and one car in line for the third.

Importantly, during one minute, the following two things happen in every lane:

  • if any cars are in that lane's line, exactly one of the cars is tolled (and leaves the line).
  • any number of new cars can arrive.

Cars cannot change lanes once they arrive in a particular toll lane.

Therefore, from the lane_lines example above, here's what we can reconstruct happened:

  • during the first minute: one car arrived to each of the first two lanes
  • during the second minute: one car was tolled in each of the first two lanes, and two cars arrived in the third lane
  • during the third minute: two cars arrived in the first lane, three cars arrived in the second lane, and one car was tolled in the third lane

Your Task

Given historical data of this form, you want to know how many distinct cars arrived at the toll checkpoint (in any lane).

Answer the below questions manually, before writing code, to check your understanding.

For the lane_lines example above, how many distinct cars arrived at the toll checkpoint?

lane_lines2 = [[2, 3, 2, 2],
               [2, 3, 2, 2],
               [1, 2, 1, 1],
               [9, 1, 0, 0], 
               [8, 0, 0, 0]]

How many lanes are in the toll checkpoint for which lane_lines2 stores historical data?

How many minutes of data are represented in lane_lines2?

How many distinct cars arrived according to the data in lane_lines2? Note that cars which are present at the beginning of the first minute of data should not be counted as arrivals.

Now, write a function num_arrivals(lane_lines) which returns the number of distinct cars that arrive according to the data in lane_lines. Note again that cars present in the very first entry of the data are not counted as arrivals.

Code Submission

Once you have confirmed your code works via your own small test cases, upload it below:

  No file selected

Next Exercise: Toll Lanes: Behavior Hypothesis

Back to exercises


 
Footnotes

1Source: [https://www.flickr.com/photos/massdot/4293675322](https://www.flickr.com/photos/massdot/4293675322). Noncommercial reuse allowed. (click to return to text)

2Your toll checkpoint does not have EasyPass or other special toll lanes like that. (click to return to text)