Log Aggregator
Part of the Logic & Flow section of Coddy's Swift journey — lesson 55 of 56.
Challenge
MediumYou'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:
- One line per level that appeared at least once, sorted in this fixed order:
ERROR,WARN,INFO. Each line is<level>: <count> (<bytes>)wherecountis the number of valid entries at that level andbytesis the sum of their bytes. Top user: <name> (<bytes>): the user with the most total bytes across all valid entries. Break ties by name alphabetically.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: 2Try 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
1Strings In Depth
Count and IndicesCase and TrimSearching in StringsSplitting and JoiningReplacing SubstringsRecap - Username Check