Space Python Loading Python…

Alert deduplicator

Write the codeHard 2 stages

When an engine runs hot, its sensor raises the same alert every few seconds, and the flight controllers' screen fills up with copies until the one alert that matters scrolls away. Every monitoring system worth using groups repeats.

Collapse repeats within 60 seconds into one line with a count, and keep everything in the order it happened.

Collapse repeats of the same alert, the same source and message, that come within WINDOW_S seconds of the first one into a single line with a count. A repeat after that starts a new line. Keep the lines in the order their first alert appeared:

09:30:05 ENG-2 Temperature high (x3)
09:30:31 TANK-1 Pressure dropping (x2)
09:31:10 ENG-2 Temperature high
09:31:40 COMMS Signal weak
09:33:00 TANK-1 Pressure dropping
09:33:05 COMMS Signal weak (x2)

Keep the groups as dicts in a list, groups, with first, last, source, message and count, and a dict from (source, message) to the newest group for that alert. to_seconds() is given.

Six lines
The groups list

Finish stage 1 to unlock this stage.

For a line that collapses several alerts, show the time of the first and the last, and finish with how many alerts came in and how many lines went out:

09:30:05-09:30:52 ENG-2 Temperature high (x3)
09:30:31-09:31:15 TANK-1 Pressure dropping (x2)
09:31:10 ENG-2 Temperature high
09:31:40 COMMS Signal weak
09:33:00 TANK-1 Pressure dropping
09:33:05-09:33:20 COMMS Signal weak (x2)
Alerts in: 10, lines out: 6
Ranges and totals
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:].