Open the context
Choose the project folder and name the files or behavior involved.
Select a reservoir of at most 32 items from a finite stream of at most 1000 items using a local seeded generator. Repeat the seed to check reproducibility and reject a stream shorter than the sample, leaving input lists unchanged.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from random import Random
def reservoir_sample(items, size, seed):
if type(size) is not int or not 0 <= size <= 32:
raise ValueError("size.range")
if type(seed) is not int or not 0 <= seed < 2**32:
raise ValueError("seed.range")
rng, sample, count = Random(seed), [], 0
for index, item in enumerate(items):
count = index + 1
if count > 1000:
raise ValueError("stream.limit")
if index < size:
sample.append(item)
elif size:
position = rng.randrange(count)
if position < size:
sample[position] = item
if size > count:
raise ValueError("stream.short")
return sampleThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.