← Back to course
Code Crew G4 / Week 06 / Thursday
4/6
Week 06 Β· Split and join

Thursday

Decode a log entry
// Decode the comms log
⏱ about 15 min

Thursday: Decode a log entry

The comms log is full of entries like LAUNCH;GATE 4;0900;READY. Each one packs four facts into one string, with a semicolon between them.

Rocket squints. "Which part is the gate? Which part is the time?"

Raven points at the semicolons. "What do you notice? The gate is always the second piece, and the time is always the third." Nova draws four glowing boxes. "Would you like a hint? Cut first. Then use an index."

Split, then index

Each log entry has the same shape. That makes it easy to decode.

entry = "LAUNCH;GATE 4;0900;READY"
parts = entry.split(";")
print(parts[1])
print(parts[2])
print(len(parts))
print(" | ".join(parts))
The screen shows
GATE 4
0900
4
LAUNCH | GATE 4 | 0900 | READY
Index0123
Meaningeventgatetimestatus

The space inside GATE 4 stays. split only cuts at the separator you give it.

entry = "DOCK;BAY 2;1430;WAIT"
parts = entry.split(";")
print(parts[0])
print(parts[-1])
First line on the screen
Second line on the screen
Which index holds the time 1430?
entry = "LAUNCH;GATE 7;1100;READY"
parts = entry.split(";")
if parts[3] == "READY":
    print("Go at", parts[2])
else:
    print("Hold")
What does the program above show?
WHY THIS EXERCISEOnce the data is split, if can make choices about each piece.
WRONG SEPARATOR
  • Read the question.
  • Tap your answer.
Rocket writes entry.split(",") for a log entry that uses semicolons. How many items are in the list?
Try it
Write a log entry about your day, like BREAKFAST;KITCHEN;0730;DONE.
Label each piece with its index.

Practice lab: decode a refuel entry

entry = "REFUEL;PAD 3;1615;DONE"
parts = entry.split(";")
print(parts[0])
print(parts[1])
print(parts[2][0:2])
What does line 3 show?
What does line 4 show?
What does line 5 show?
How many pieces are in parts?
READ THE ENTRY
  • Read the question.
  • Tap your answer.
Which index holds the status DONE?
What does parts[2][2:4] give?
Which separator splits this entry?
Statement about the refuel entryTrue or false?
split(";") cuts the space inside PAD 3.?
parts[-1] is "DONE".?
len(parts) is 4.?
parts[1] is "REFUEL".?
WHY THIS EXERCISEKnowing exactly what split keeps is the key to decoding data.
Split "SCAN;RING 2;0815;HOLD" at the semicolons. Which piece is at index 3?
WHY THIS EXERCISEThe status is always the fourth piece, at index 3.

You decoded the log! Tomorrow, a hidden message is waiting inside it.

← Wednesday