MediumEvaluationPython 3

Experiment Traffic Splitter

Assign stable experiment variants using weighted deterministic buckets.

30m3 sample tests6 hidden tests

Assign users to weighted experiment variants with stable deterministic bucketing.

Requirements

  • Define assign_variant(user_id, experiment_key, variants).
  • variants is a list of (name, weight) tuples.
  • Ignore variants with non-positive weight (weight <= 0); only weight > 0 contributes 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 total is the sum of positive weights
  • Map bucket onto cumulative weight ranges in input order: walk variants left to right, accumulate weight, and return the first name where bucket < cursor.
  • Return the selected variant name.
  • Return None if total positive weight is 0.
  • Changing experiment_key can 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"} 2# With the required bucket formula: 3assert assign_variant("alice", "ranking", [("a", 1), ("b", 3), ("c", 6)]) == "c" 4assert assign_variant("same-user", "exp-a", [("a", 2), ("b", 2), ("c", 2)]) == "b"

Constraints

  • Don't use Python's built-in hash, because it's process-randomized.
  • Don't use randomness.
  • Preserve variant order for bucket ranges.

Editor