…
…
Generate every departure time from the campus shuttle timetable; find the next bus at a stop, the earliest arrival on a trip with a transfer, and warn before the last bus of the night is missed.
Three shuttle buses run around Greenhill Campus: Red (R1), Blue (R2), and Green (R3). The signs at the stops only say "first bus 07:30, last bus 23:10, every 20 minutes"; nobody works out when the bus will pass your stop, or when you would arrive if you made a transfer. The student council wants a small planner that does this calculation. The timetable is in the file timetable.json: each route's stops, first and last departure, interval, and the times between stops.
At the end of the program, you will be able to answer questions like these:
OutputR1 Library 08:00: 08:17 (17 min wait)
R1 Main Gate 08:10 -> Residence Halls 08:22
R2 Residence Halls 08:27 -> Tech Park 08:37
Arrival with transfer: 08:37 | on R3 without a transfer: 08:40
Hurry: last R1 at 23:22, 22 min left.Comparing times as strings is a sneaky mistake: "9:05" < "10:00" gives False. In this project, you will convert every time to a datetime and add durations with timedelta.
Lessons used: conditions (6–8), while, continue (11), accumulating (12), functions, default parameters (14–16), indexing and slices (20), formatting (22), lists (23–24), dictionaries (26–28), tuples and set intersection (29), comprehensions (30), min and key, lambda (31), raise ValueError and try–except (32), JSON files (35), import (36), datetime, timedelta, strptime, strftime (37).
How to work: The seven cells share one session: read_time and departure_times in cell 2 and stop_offset in cell 3 are the building blocks of the later cells; each function is built on top of the one before. The comments tell you what to do, and you write the code where it says ___. Run the cell, compare your output with the "Show expected output" area, then press Check. The checks also try your functions with other routes, stops, and times; code that writes the result by hand does not pass.
What you'll do
timetable.jsonDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The campus shuttle information is in the file timetable.json. For each route it has the code (R1), the name, the stops in order, the first and last departure times, how often a bus leaves (interval_min), and the times between consecutive stops (leg_minutes). The first element of the leg_minutes list is the minutes from stop 1 to stop 2, the second is from stop 2 to stop 3, and so on; that is why it has one element fewer than the number of stops.
Read the file, then build a dictionary so you can reach each route by its code: routes["R1"] should give all the information about R1. The loop below summarizes each route in one line.
This step's code cell is for members.Sign up free
The timetable gives only the first and last departures and the interval; you will generate the departures in between. First, read_time should turn a time written as a string ("07:30") into a datetime
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
departure_times(route)interval_minDo not add up times as strings or compare them as strings: "9:15" < "10:05" gives False (lesson 37). With datetime, you also do not have to work out the minutes rolling over at 60.
This step's code cell is for members.Sign up free
A departure time is the moment the bus leaves the first stop; a student waiting at a stop in the middle needs the time the bus passes that stop.
stop_offset(route, stop) should return how many minutes it takes to get from the first stop to this stop: if the stop's index in the list is i, it is the sum of the first i elements of leg_minutes. For the first stop, the result is 0. If the stop is not on this route, it should raise a ValueError instead of quietly returning a wrong number; the message is <stop> is not a stop on route <route code>.
departures_at_stop(route, stop) should add this offset to every departure time and return the list.
This step's code cell is for members.Sign up free
A student looking at the time at a stop has only one question: when is the next bus? next_bus(route, stop, time_text) should return the first time among the times at the stop that is not before the given time. If the bus arrives at exactly that minute, you can catch it, so equality counts. If there is no bus after that time, it should return None.
Convert the time with read_time again; the student may type 8:17 or 08:17.
This step's code cell is for members.Sign up free
Now plan a trip with a boarding stop and an alighting stop. The shuttles run one way: the bus visits the stops in the order of the list. trip(route, board, alight, time_text):
1. Raises a ValueError if the alighting stop does not come after the boarding stop; the message is <alight> does not come after <board>.
2. Finds the next bus at the boarding stop; returns None if there is none.
3. Arrival time = boarding time + the difference between the stop_offset of the two stops. Returns the tuple (boarding_time, arrival_time).
This step's code cell is for members.Sign up free
A student who wants to go from Main Gate to Tech Park can board R1 and transfer to R2. R1 and R2 have two stops in common: Library and Residence Halls. Which is the better place to get off? The first idea is "get off at the first shared stop"; but R2 goes from Library to Residence Halls by way of Sports Hall, while R1 takes the shortcut. So a student who transfers at Residence Halls can catch an earlier R2.
transfer_trip(route1, board, route2, alight, time_text) should try all the shared stops and return the option that arrives earliest. Changing buses takes TRANSFER_MIN minutes: you wait for the second bus from 3 minutes after you get off the first. If a shared stop is not after the boarding stop on the first route and before the alighting stop on the second, skip that stop. Each option is a tuple (arrival, transfer, first, second); first and second are the tuples returned by trip from step 5.
This step's code cell is for members.Sign up free
Write a warning for a student who loses track of time while studying in the library. last_bus_warning(route, stop, time_text, threshold_min=30) should look at the last bus that passes the stop and return one of three messages:
Last R1 left at 23:22; no more service today.threshold_min minutes or fewer are left until the last bus: Hurry: last R1 at 23:22, 22 min left.Relax: last R1 at 23:22.If the bus passes right now, you have not missed it yet: "0 min left". threshold_min is a parameter with a default value (lesson 15); the caller can change it if they want.
This step's code cell is for members.Sign up free