Reliability algorithms

Failure streak opening, partly written

Locate the first point at which consecutive failures would open a simplified circuit breaker.

In production

Circuit breakers open on consecutive failures, not lifetime totals. Counting the wrong thing trips early or never.

What it must return

Return the index where the consecutive failure streak first reaches threshold, or the refusal the examples use when it never does.

  • outcomes contains booleans: True is success, False is failure. threshold is a positive integer. Return the zero-based index of the first threshold-length failure streak, or None. Success resets the streak.
  • Do not mutate input values. Use the Python standard library only.

Edge cases

  • A success resets the streak to zero.
  • Total failure count is not the streak.
  • Threshold is consecutive outcomes.

Signature

opening_index(outcomes, threshold)

Part of this function is already written. The TODO comments mark what is left.

Hints

  1. Second hint

  2. A partial sketch

The reference solution opens after the last hint.

solution.py · Python

Loading editor…

Consecutive failures

outcomes

[
  false,
  true,
  false,
  false
]

threshold

2

Expected

3

Each run uses a fresh Python runtime in this browser, with a 3-second limit and a Stop button. The first run downloads the runtime. 0 of 10 in Reliability algorithms passed in Python on this device.

Next: Failure streak opening, with a bug