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."
To decompose a problem is to break it into smaller jobs. Each job can become a function or a short loop.
| Job | Tool you already know | Week |
|---|---|---|
| Decode a log line into a record | split, index, int, dictionary | 6 and 7 |
| Keep only the ready launches | loop, if, append | 3, 4 and 8 |
| Add up the fuel | function with return, running total | 3 and 10 |
| Print the flight board | loop over records, print | 8 |
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 |
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"]) | Statement | True 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. | ? |
Great planning, Crew Engineer! Tomorrow you will build the schedule from the log.