Fix the model of which job each goroutine runs
Your task
An AI explained a Go fan-out loop as "one goroutine per job, each processing its own job". Each goroutine is a closure that reads the loop variable job. Whether every closure gets its own copy of job depends on the Go version, so your team models which job each goroutine really processes before building on the code.
An assistant wrote resolve_captured_jobs(job_ids, run_after_index, go_minor). It is in the editor. It has a bug. Find it and fix it.
Input and output:
job_ids: the jobs, in loop order. Goroutinekis launched on iterationk, forjob_ids[k]. Ids can repeat.run_after_index: for goroutinek, the loop iteration the loop had reached when that goroutine ran. It can be shorter thanjob_ids, and its values can be any integers.go_minor: the minor Go version in the module'sgo.modfile.20means Go 1.20.- Return one job id per goroutine, in launch order: the job that goroutine processed.
Rules the tests check:
- With no jobs, return an empty list.
- Go 1.22 and later (
go_minor22 or more): each iteration has its own loop variable, so goroutinekprocessesjob_ids[k]. - Before Go 1.22 (
go_minor21 or less): every goroutine shares one loop variable, so goroutinekprocesses the job at indexrun_after_index[k].- When
run_after_indexhas no entry fork, the goroutine ran after the loop finished: it processes the last job. - Clamp the index to the valid range: below 0 counts as 0, and above the last index counts as the last index.
- When
Examples
resolve_captured_jobs(job_ids: list[str], run_after_index: list[int], go_minor: int) -> list[str]Example 1
Input
resolve_captured_jobs(["j1", "j2", "j3"], [2, 2, 2], 20)Output
["j3", "j3", "j3"]Example 2
Input
resolve_captured_jobs(["j1", "j2", "j3"], [2, 2, 2], 22)Output
["j1", "j2", "j3"]