Audio guide dwell
MediumOnline Assessment
Work out how long visitors spend at each museum exhibit from the log of their audio guides.
Every audio guide reports events as strings "<seconds> <guide> <action> [<exhibit>]", such as "540 g7 enter night-watch". seconds counts from opening time. The actions are enter <exhibit>, leave <exhibit> and return (the guide was handed back at the desk). The log was merged from several sources, so it is not in time order.
Process events in increasing time order; events with the same time keep their order in log. A guide has at most one open visit at a time:
enter Xstarts a visit toX. If the guide already has an open visit, that visit ends at this same moment first.leave Xends the open visit only if it is a visit toX. Otherwise (no open visit, or a visit to a different exhibit) the event is ignored.returnends the open visit, if there is one, and is otherwise ignored.- A visit still open after the last event is discarded: nobody knows when it ended.
- An ended visit lasts
end - startseconds. Visits lasting fewer than 10 seconds are walk-bys and are not counted at all.
Input: log, a list of event strings.
Output: a list with one entry [exhibit, visits, totalSeconds, averageSeconds] per exhibit that has at least one counted visit. averageSeconds is totalSeconds // visits (rounded down). Order entries by totalSeconds descending; exhibits with equal totals are ordered alphabetically by name. Return [] if nothing was counted.
Example 1
- Input:
- [["170 g1 enter vase","100 g1 enter rodin","105 g2 enter rodin","160 g1 leave rodin","108 g2 leave rodin","400 g1 return","200 g2 enter vase"]]
- Output:
- [["vase",1,230,230],["rodin",1,60,60]]
- Explanation
- Sorted by time, guide
g1entersrodinat 100 and leaves at 160 (60 s), entersvaseat 170 and is handed back at 400 (230 s). Guideg2spends 3 s atrodin, a walk-by that is ignored, and never returns after enteringvaseat 200, so that visit is discarded.vaseranks first with 230 s.
Example 2
- Input:
- [["50 g1 enter harp","90 g1 enter vase","120 g1 leave harp","150 g1 return","200 g2 enter harp","210 g2 leave harp"]]
- Output:
- [["vase",1,60,60],["harp",2,50,25]]
- Explanation
g1entersharp, then entersvaseat 90 without leaving: the harp visit ends at 90 (40 s) and counts. Theleave harpat 120 finds the guide atvase, so it is ignored, andreturnat 150 closes the vase visit (60 s). The visit of exactly 10 s atharpforg2counts, soharptotals 50 s over two visits andvase(60 s) ranks above it.
Example 3
- Input:
- [["5 g1 leave rodin","6 g1 return","10 g1 enter vase","14 g1 leave vase","20 g1 return"]]
- Output:
- []
- Explanation
- A
leaveorreturnwith no open visit is ignored, and the one real visit of 4 s is a walk-by, so nothing is reported.
Constraints
0 <= log.length <= 2,000- each entry is
"<seconds> <guide> <action> [<exhibit>]";secondsis an integer from0to86,400 actionisenter,leaveorreturn; onlyenterandleavecarry an exhibit name (lowercase letters and hyphens)