Open lesson

Mission: the depot with two names

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

Brief

Choose a join strategy for the table sizes and prevent false links from a key reused over time.

Voss waves a table of seven 'transport proofs' for four trips. Imani notices D7 changed name at 20:00. Mara asks you to rebuild the join on a copy before the Hidden Depot map is shown to the team. The suspense belongs to the story; the exercise has no timer.

Your mission

  1. Explain why joining only on depot_id produces seven rows.
  2. Match T1, T2, T3, and T4 to the place valid at their times.
  3. Choose between broadcasting a tiny depot table and sort-merging two large tables, naming a cost of each.
  4. Give a check that immediately detects false matches.

How to check your work

  • Three D7 trips × two versions + one D8 trip = seven naive rows.
  • T1 North; T2 and T3 Hidden Depot; T4 South.
  • Broadcast saves moving the large side but uses memory; sort-merge moves and sorts data.
  • Four distinct trip_id values → four rows, one dated description per trip.

Course example

trips = [
    ("T1", "D7", 1170),
    ("T2", "D7", 1285),
    ("T3", "D7", 1300),
    ("T4", "D8", 1310),
]
depots = [
    ("D7", "North Yard", 0, 1200),
    ("D7", "Hidden Depot", 1200, 1440),
    ("D8", "South Yard", 0, 1440),
]
naive = [(trip[0], depot[1]) for trip in trips for depot in depots if trip[1] == depot[0]]
temporal = [
    (trip[0], depot[1])
    for trip in trips for depot in depots
    if trip[1] == depot[0] and depot[2] <= trip[2] < depot[3]
]
print("naive_rows", len(naive))
print("temporal_rows", len(temporal))
print("temporal_matches", temporal)
print("one_match_per_trip", len({trip_id for trip_id, _ in temporal}) == len(trips))

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with # are task reminders. Python ignores them. Write your code beneath them.

Python console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.py

Current editor source. Inspection shows your current edits.

# Explain why joining only on depot_id produces seven rows.
# Match T1, T2, T3, and T4 to the place valid at their times.
# Choose between broadcasting a tiny depot table and sort-merging two large tables, naming a cost of each.
# Give a check that immediately detects false matches.

exemple.py

Declarative lesson asset; this is not a file created in the Python filesystem.

trips = [
    ("T1", "D7", 1170),
    ("T2", "D7", 1285),
    ("T3", "D7", 1300),
    ("T4", "D8", 1310),
]
depots = [
    ("D7", "North Yard", 0, 1200),
    ("D7", "Hidden Depot", 1200, 1440),
    ("D8", "South Yard", 0, 1440),
]
naive = [(trip[0], depot[1]) for trip in trips for depot in depots if trip[1] == depot[0]]
temporal = [
    (trip[0], depot[1])
    for trip in trips for depot in depots
    if trip[1] == depot[0] and depot[2] <= trip[2] < depot[3]
]
print("naive_rows", len(naive))
print("temporal_rows", len(temporal))
print("temporal_matches", temporal)
print("one_match_per_trip", len({trip_id for trip_id, _ in temporal}) == len(trips))