User variant rollout rules

Write codeEasy16 min

Your task

A rollout service picks which build of the app each user sees. Every rule is written "conditions|variant", where conditions is either "*", which matches everyone, or a comma separated list of "key=value" pairs that must all hold. Every user is written "id;key=value;key=value": the first field is the user id, the rest are attributes, and a key written twice keeps its last value. A rule holds when each of its conditions names an attribute the user carries with exactly that value, compared as text. Rules are tried in the order given and the first one that holds decides the variant, even when a later rule is narrower. A user no rule holds for gets fallback. Return the variant for each user, in the order the users are given.

Examples

assign_rollout_variants(rules: list[str], users: list[str], fallback: str) -> list[str]
Example 1
Inputassign_rollout_variants(["plan=pro,country=US|beta", "plan=pro|early", "country=US|us_only"], ["u1;plan=pro;country=US", "u2;plan=pro;country=CA", "u3;plan=free;country=US", "u4;plan=free;country=DE"], "control")
Output["beta", "early", "us_only", "control"]
Example 2
Inputassign_rollout_variants(["*|everyone", "plan=pro|beta"], ["u1;plan=pro", "u2;plan=free"], "control")
Output["everyone", "everyone"]
Example 3
Inputassign_rollout_variants(["plan=pro|beta"], ["u1;country=US", "u2;plan=pro"], "control")
Output["control", "beta"]