Log analyser
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.
Finish stage 1 to unlock this stage.
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".
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.
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).
Hints
Iterating Over Dictionary
# 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")
Separate arguments with commas. They're passed on every Run as sys.argv[1:].