Workspace/Coding labs
Loading progress

Count distinct events in a half-open window

Intermediate45 min

Implement window_counts(events, start, end), counting first-seen logical events by entity within [start,end). A logical event key is (source,id).

Your task

  1. Reject end <= start with ValueError.
  2. Deduplicate before window filtering: the first occurrence of a logical key determines its entity and time.
  3. Count only events whose time is at least start and strictly below end.
  4. Return a dictionary of entity counts, omit zero-count entities, and do not mutate events.

Examples

EXAMPLE 1

InputTwo deliveries of s1/e1 at time 10 for api; window [0,20)

Output{'api': 1}

Transport repetition does not create a second logical event.
solution.pyPython 3.12