EasyPython

Throttle (Simulation)

PythonAsyncFunctions

Real throttling is hard to test deterministically with timers. Instead,
implement solve(events, interval) that simulates a leading-edge throttle.

events is a list of [timestampMs, value] pairs representing calls to a
throttled function. Return only the [timestamp, value] pairs that would
actually fire (one at most per interval window).

Semantics (leading edge)

  • The first call in a window fires immediately.
  • Subsequent calls within the same window are dropped.
  • A new window starts interval ms after the last fired call.

solve([[0,'a'],[5,'b'],[10,'c'],[20,'d']], 15)[[0,'a'],[20,'d']]

Sample tests

Test #1Basic leading-edge throttle
Input: [[[0,"a"],[5,"b"],[10,"c"],[20,"d"]],15]
Output: [[0,"a"],[20,"d"]]
Test #2All within one window
Input: [[[0,1],[1,2],[2,3]],10]
Output: [[0,1]]
Test #3Each call starts a new window
Input: [[[0,1],[10,2],[20,3]],10]
Output: [[0,1],[10,2],[20,3]]
Test #4Empty events
Input: [[],100]
Output: []
Test #5Single event always fires
Input: [[[0,"x"]],5]
Output: [[0,"x"]]