MediumEvaluationPython 3
Experiment Traffic Splitter
Assign stable experiment variants using weighted deterministic buckets.
30m3 sample tests6 hidden tests
If a user returns to the same experiment, the assigned variant should stay the same for the same experiment key, ordered variants, and weights. Use weighted experiment variants with stable deterministic bucketing.
Requirements
- Define
assign_variant(user_id, experiment_key, variants). variantsis a list of(name, weight)tuples.- Names, user IDs, and experiment keys are strings, and weights are integers.
- Ignore variants with non-positive weight (
weight <= 0); onlyweight > 0contributes to ranges. - Use this exact stable bucket:
key = f"{experiment_key}:{user_id}"bucket = sum((index + 1) * ord(char) for index, char in enumerate(key)) % total- where
totalis the sum of positive weights
- Iterate Unicode code points, not UTF-16 code units. In Java, advance with
codePointAtandCharacter.charCountso emoji receive the same bucket as Python. - Map
bucketonto cumulative weight ranges in input order: walk variants left to right, accumulate weight, and return the first name wherebucket < cursor. - 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.
- Stability applies to identical keys, ordered variants, and weights. Reordering variants or changing weights can reassign users.
Example
python
assert assign_variant("u1", "exp", [("control", 50), ("treatment", 50)]) in {"control", "treatment"}
# With the required bucket formula:
assert assign_variant("alice", "ranking", [("a", 1), ("b", 3), ("c", 6)]) == "c"
assert assign_variant("same-user", "exp-a", [("a", 2), ("b", 2), ("c", 2)]) == "b"
assert assign_variant("anyone", "exp", [("disabled", -1), ("enabled", 1)]) == "enabled"Constraints
- Don't use Python's built-in
hash, because it's process-randomized. - Don't use randomness.
- Preserve variant order for bucket ranges.
Production assignment services typically replace weighted character sums and raw colon concatenation with length-prefixed keys and uniform hashes such as MurmurHash3 or SHA-256; use the exact polynomial formula above here so bucket indices match the test vectors.
Editor