Fix the release guard in a particle pool
Your task
Your particle system reuses particles from a pool. A collision handler released the same particle twice, so the pool held it twice and later handed one particle to two effects at once. The fix makes a release count only for a particle that is currently checked out.
An assistant wrote run_pool_ops(ops). It is in the editor. It has a bug. Find it and fix it.
Input and output:
ops: the pool operations in order, each"get"or"release:<id>".- Return the id each
"get"hands out, in order.
Rules the tests check:
- A get takes the most recently released particle (last in, first out). If none is free, it creates a new particle. New ids are 1, 2, 3 and so on, in creation order.
- A particle is checked out from the get that hands it out until a release of its id.
"release:<id>"needs one or more ASCII digits after the colon, read as a number. A release puts the particle back in the pool only if it is checked out right now. Any other release does nothing, including a second release and a release of an id the pool never handed out.- Any other op is ignored.
Examples
run_pool_ops(ops: list[str]) -> list[int]Example 1
Input
run_pool_ops(["get", "get", "release:1", "get"])Output
[1, 2, 1]Example 2
Input
run_pool_ops(["get", "get", "release:1", "release:1", "get", "get"])Output
[1, 2, 1, 3]