← Back to course
Code Crew G4 / Week 12 / Monday
1/6
Week 12 Β· Launch day

Monday

Plan the planner
// Build the launch planner
⏱ about 15 min

Monday: Plan the planner

Launch day is Friday! The station needs a launch planner. It must read the comms log and find the ready launches. Then it checks the fuel and prints the flight board.

"Let's build it all right now!" Rocket says, cracking his knuckles. Raven holds up a hand. "Wait. What do you notice? That is four jobs, not one."

Nova draws four glowing boxes in a row. "Would you like a hint? Big programs are built from small pieces. Build one piece, test it, then build the next."

Decompose the problem

To decompose a problem is to break it into smaller jobs. Each job can become a function or a short loop.

JobTool you already knowWeek
Decode a log line into a recordsplit, index, int, dictionary6 and 7
Keep only the ready launchesloop, if, append3, 4 and 8
Add up the fuelfunction with return, running total3 and 10
Print the flight boardloop over records, print8

Piece 1: decode

Each log entry looks like Moon hop;2;0900;30;READY. The pieces are name, gate, time, fuel and status.

def decode(line):
    parts = line.split(";")
    return {
        "name": parts[0],
        "gate": parts[1],
        "time": parts[2],
        "fuel": int(parts[3]),
        "status": parts[4].upper(),
    }

launch = decode("Moon hop;2;0900;30;READY")
print(launch["name"], "needs", launch["fuel"])
print(launch["fuel"] + 10)
The screen shows
Moon hop needs 30
40
  • int(parts[3]) turns the fuel string into a number, so the program can add it.
  • parts[4].upper() makes every status capitals. That will matter on Thursday.
decode("Mars run;4;1100;70;HOLD")["gate"] gives
decode("Mars run;4;1100;70;HOLD")["time"] gives
PUT THE PLANNER'S JOBS IN THE ORDER THE PROGRAM DOES THEM
  • ?Print the board and the budget check
  • ?Keep only the ready launches
  • ?Decode each log line into a record
  • ?Add up their fuel
WHY THIS EXERCISEPlanning the order of jobs is the first step of every big program.
Try it
Pick a big job at home, like getting ready for a trip.
Break it into four smaller jobs.

Practice lab: decode two lines

def decode(line):
    parts = line.split(";")
    return {"name": parts[0], "gate": parts[1], "fuel": int(parts[3])}

a = decode("Ring tour;7;1400;25;ready")
b = decode("Comet chase;5;1600;40;READY")
print(a["name"], a["gate"])
print(a["fuel"] + b["fuel"])
What gate number does line 7 show?
What does line 8 show?
What is b["name"]?
DECODE CHECK
  • Read the question.
  • Tap your answer.
Why does decode use int on parts[3]?
For the Ring tour line, what is parts[0]?
What does it mean to decompose a problem?
StatementTrue or false?
decode returns a dictionary.?
a["gate"] is a number.?
Each job of the planner can be tested alone.?
The order of the jobs does not matter.?
WHY THIS EXERCISEKnowing which values are strings and which are numbers prevents bugs later.
How many pieces does "Sun watch;1;1800;30;READY".split(";") make?
WHY THIS EXERCISEEvery log line has five pieces: name, gate, time, fuel and status.

Great planning, Crew Engineer! Tomorrow you will build the schedule from the log.