Space Python Loading Python…

Log analyser

Write the codeHard 3 stages

Launch day produced a long mission log, and the post-flight review starts in an hour. Nobody will read every line. The review board wants three answers: how busy was each hour and level, what went wrong first, and which subsystem complained the most.

Build the analyser that answers them, one stage at a time.

Split each line into its time, level, subsystem and message with split(maxsplit=3), so the message stays whole, and keep them as dicts in entries, with the time as "HH:MM:SS". Then count the entries per level, most common first, and per hour, in time order:

By level:
  WARN: 6
  INFO: 5
  ERROR: 2
By hour:
  09:00: 4
  10:00: 4
  11:00: 5

Two Counters do the counting; the hour is the first two characters of the time, plus ":00".

Levels and hours
Parsed entries

Finish stage 1 to unlock this stage.

Find the first ERROR entry and print its time, subsystem and message, or No ERROR entries when there are none:

First ERROR: 10:14:47 PROP Fuel valve 2 not responding

Stop the loop with break as soon as you find it. The hidden tests also run a log without any errors.

The first ERROR

Finish stage 2 to unlock this stage.

Rank the subsystems by how many WARN entries they raised, numbered from 1:

Warnings by subsystem:
  1. POWER: 3
  2. GNC: 2
  3. COMMS: 1

enumerate(warnings.most_common(), start=1) gives the rank and each (subsystem, count) pair; unpack them as rank, (subsystem, count).

The ranking
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

Iterating Over Dictionary

iterating-over-dictionary.py
# How long a single day lasts on each planet, measured in Earth hours
day_hours = {
    "Mercury": 1408,
    "Venus": 5832,
    "Earth": 24,
    "Mars": 24.6,
    "Jupiter": 9.9,
}

# A plain for loop walks the keys
for planet in day_hours:
    print("Planet:", planet)

# .values() walks the values instead
total = 0
for hours in day_hours.values():
    total = total + hours
print("All five days added together:", total, "hours")

# .items() hands you the key and the value at the same time
for planet, hours in day_hours.items():
    print(f"{planet}: one day is {round(hours / 24, 2)} Earth days")

# sorted() walks the same keys in a different order
for planet in sorted(day_hours):
    print(planet, "->", day_hours[planet], "hours")
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].