Kumita← Open visual lesson

Cascades

The code behind the picture

Follow the lesson’s implementation, one code cell at a time.

Choose a language and read the cells in order. These are small teaching examples; the visual lesson runs separately.

Follow the calculation

Same inputs · Same operation · Same result

3 teaching cells · Python standard library · Read-only

  1. 1. Prepare the inputs

    Use these small, explicit inputs to follow the calculation.

    import json
    
    scores = [[0.9,0.8,0.7], [0.2,0.9,0.9], [0.9,0.1,0.8]]
    threshold = 0.5
  2. 2. Stop at the first failed check

    The scores and cutoffs are supplied for this example. Early exit gives the same survivors as evaluating every check, with fewer calls.

    calls = 0
    survivors = []
    for index, checks in enumerate(scores):
        for score in checks:
            calls += 1
            if score < threshold:
                break
        else:
            survivors.append(index)
    result = {"survivors": survivors, "calls": calls, "without_early_exit": 9}
  3. 3. Inspect the result

    Print the result as JSON so both languages use the same output format.

    print(json.dumps(result))

Expected result

Running the three cells in order produces this result in either language.

{
  "survivors": [
    0
  ],
  "calls": 6,
  "without_early_exit": 9
}