Menu
Coddy logo textTech

Log Aggregator

Part of the Logic & Flow section of Coddy's Swift journey — lesson 55 of 56.

challenge icon

Challenge

Medium

You're processing access logs. Read a single line of input: a comma-separated list of level:user:bytes entries.

Some entries are bad and should be skipped: any entry whose bytes can't be parsed as an integer, or whose level isn't one of INFO, WARN, ERROR (case-sensitive).

Print, in this order:

  1. One line per level that appeared at least once, sorted in this fixed order: ERROR, WARN, INFO. Each line is <level>: <count> (<bytes>) where count is the number of valid entries at that level and bytes is the sum of their bytes.
  2. Top user: <name> (<bytes>): the user with the most total bytes across all valid entries. Break ties by name alphabetically.
  3. Skipped: <n> with the count of malformed entries.

For input INFO:alice:100,WARN:bob:bad,ERROR:alice:50,INFO:bob:200,DEBUG:cara:10,INFO:alice:75, the output is:

ERROR: 1 (50)
INFO: 3 (375)
Top user: alice (225)
Skipped: 2

Try it yourself

let entries = readLine()!.components(separatedBy: ",")
let order = ["ERROR", "WARN", "INFO"]
let validLevels: Set<String> = ["INFO", "WARN", "ERROR"]

// TODO: parse each entry, drop bad ones, accumulate per-level + per-user;
// print levels in fixed order (only if seen), top user, then Skipped count

All lessons in Logic & Flow