HardPro challengePython

asyncio.Semaphore — Rate-Limit Concurrent Tasks

PythonAsyncConcurrency

Implement solve(items, limit) that processes a list of integers concurrently but allows at most `limit` tasks running at the same time.

Each "task" doubles its input value. Return the results in the same order as input.

Examples

  • solve([1, 2, 3, 4, 5], 2)[2, 4, 6, 8, 10]
  • solve([10], 1)[20]

Constraints

  • Use asyncio.Semaphore(limit) to cap concurrency.
  • Results must preserve input order.

Sample tests

Test #1Single item
Input: [[10],1]
Output: [20]
Test #2Five tasks, limit 2
Input: [[1,2,3,4,5],2]
Output: [2,4,6,8,10]
Test #3Limit equals count — all run at once
Input: [[1,2,3],3]
Output: [2,4,6]