Token bucket with fractional refill
Your task
Implement a token-bucket rate limiter. The bucket holds at most capacity tokens and refills at refill_rate tokens per second (fractional accrual allowed). It starts full at capacity. Process a list of request events as [timestamp, cost] pairs with non-decreasing integer timestamps. Before each request, add (timestamp - last_timestamp) * refill_rate tokens, clamped at capacity. If the bucket holds at least cost tokens, allow the request and subtract cost; otherwise deny it and leave the bucket unchanged. The first event uses the bucket's initial full state with no prior refill. Return a list of booleans: True if allowed, False if denied. capacity and refill_rate are positive numbers.
Examples
token_bucket(capacity: float, refill_rate: float, events: list[list]) -> list[bool]Example 1
Input
token_bucket(5, 1, [[0, 3], [0, 3], [2, 2]])Output
[True, False, True]