MediumEvaluationPython 3
Experiment Traffic Splitter
Assign stable experiment variants using weighted deterministic buckets.
30m2 sample tests3 hidden tests
Assign users to weighted experiment variants with stable deterministic bucketing.
Requirements
- Define
assign_variant(user_id, experiment_key, variants). variantsis a list of(name, weight)tuples.- Ignore variants with weight
0. - Use a stable bucket derived from
experiment_keyanduser_id. - Return the selected variant name.
- Return
Noneif total positive weight is0. - Changing
experiment_keycan change assignment. - Calling the function repeatedly with the same inputs must return the same variant.
Example
python
1assert assign_variant("u1", "exp", [("control", 50), ("treatment", 50)]) in {"control", "treatment"}Constraints
- Do not use Python's built-in
hash, because it is process-randomized. - Do not use randomness.
- Preserve variant order for bucket ranges.
Editor
Results
Run sample tests or submit all tests.