Estimate how many users a bad version reaches
Your task
A canary puts a new version on a small slice of traffic. Before yours starts, you want to know how far a bad version could spread. If the new version is bad, each task it handles fails visibly with a chance you can estimate from the task's type. The canary controller rolls the version back as soon as it has seen k visible failures, and no later task reaches the version.
Write expected_reach(tasks, k) that returns how many different users a bad version reaches on average.
Input and output:
tasks: 1 to 3,000 tasks in arrival order, each[user, chance].useris a non-empty string compared exactly.chanceis a whole number from 0 to 100, the percent chance that this task fails visibly. Tasks fail independently of each other.k: a whole number from 1 to 200.- Return a float. The tests accept any value within 0.000001 of the exact one.
Rules:
- Tasks reach the version in order until the rollback. The task that causes the
k-th failure did reach the version, so its user counts. - A user counts once, however many of their tasks reached the version. Each of those tasks can still fail.
- If fewer than
kfailures ever happen, every task reaches the version.
Examples
expected_reach(tasks: list[list], k: int) -> floatExample 1
Input
expected_reach([["ana", 50], ["bo", 50], ["cy", 50]], 1)Output
1.75Example 2
Input
expected_reach([["ana", 100], ["bo", 0], ["ana", 0]], 2)Output
2Example 3
Input
expected_reach([["ana", 20], ["ana", 20], ["bo", 20], ["cy", 20]], 2)Output
2.856