Loops and Iteration

  1. Accumulators in Loops

    An accumulator stores a result for the part of the input a loop has processed so far.

    Undergraduate
  2. Counting Events in a Loop

    A counter increases when a specific event occurs. Its final value depends on both the input and the counting rule.

    Undergraduate
  3. Choosing Between For and While Loops

    Choose iteration over known inputs when each supplied item has work. Use a condition-driven loop when the state determines whether another step is needed.

    Undergraduate
  4. Counting with Python's Range

    A range represents an arithmetic progression with an excluded stop value and a nonzero step.

    Undergraduate
  5. Keeping the Index While Iterating

    An enumerated loop provides a position and an item together when both are needed for the same operation.

    Undergraduate
  6. Iterating Over Two Sequences Together

    Paired iteration reads corresponding positions and needs a policy when the two sources have different lengths.

    Undergraduate
  7. Breaking and Continuing a Loop

    A break exits the nearest loop. A continue skips the rest of the current iteration and proceeds according to that loop's rules.

    Undergraduate
  8. Python Loop Else Clauses

    A loop else clause runs when the loop completes normally, including a for loop that reads no items. A break skips it.

    Undergraduate
  9. Listing Pairs with Nested Loops

    Nested loops generate pairs by combining outer and inner positions. The bounds determine whether pairs repeat.

    Undergraduate
  10. Off-by-One Errors in Loops

    An off-by-one error usually changes the first or last position reached, which makes short traces especially useful.

    Undergraduate
  11. Testing Progress in a While Loop

    A condition-driven loop needs a state update that moves it toward a stopping condition within the allowed domain.

    Undergraduate
  12. Filtering Values While Iterating

    A filter retains the original items satisfying a predicate, typically in the order they were visited.

    Undergraduate
  13. Transforming Each Item in a Loop

    A mapping loop applies one transformation per input item. The result typically keeps positional correspondence with the source.

    Undergraduate
  14. Reading List Comprehensions

    A list comprehension builds a list by iterating, optionally selecting items, and evaluating the requested result expression for each selected item.

    Undergraduate
  15. Mutating a List While Iterating

    Changing a list's structure during traversal can move elements relative to the iterator and cause a step to skip an item.

    Undergraduate
  16. Finding the First Matching Item

    A first-match scan returns the earliest visited item satisfying a predicate and stops as soon as it has that witness.

    Undergraduate
  17. Computing a Running Minimum

    A running minimum stores the best value encountered at each point of a traversal and requires a valid empty-input policy.

    Undergraduate
  18. Reducing a Sequence with Any or All

    Existential and universal checks summarize a sequence of predicates and can finish before visiting every item.

    Undergraduate
  19. Computing a Sequence of Prefix Totals

    A prefix scan emits the accumulator after each input so later code can inspect cumulative work.

    Undergraduate
  20. Maintaining a Running Average

    A running average can keep a sum and an observation count rather than retaining every input.

    Undergraduate
  21. Grouping Values by a Computed Key

    To group values, compute one key per item and append the item to the collection for that key.

    Undergraduate
  22. Bounded Retry Loops

    A retry loop repeats an operation only for selected outcomes and needs a clear attempt budget and stopping rule.

    Undergraduate
  23. Iterating Through Text Lines

    A text file can provide one line at a time, which makes it possible to process input without retaining every record.

    Undergraduate
  24. Flattening One Level of a Nested Sequence

    One-level flattening concatenates the child sequences in traversal order and leaves values deeper in the structure as values.

    Undergraduate
  25. Processing Consecutive Batches

    Batching groups consecutive inputs under a size limit and must specify the treatment of the final incomplete batch.

    Undergraduate
  26. Comparing Adjacent Items

    Adjacent pairs describe transitions between neighboring values. Count them by looking at the gaps between items.

    Undergraduate
  27. Stopping a Search Across Nested Loops

    Stopping at the first matching pair requires leaving both loops or expressing the search as an operation with a result.

    Undergraduate
  28. Exhaustion of a One-Pass Iterator

    An iterator can consume its source as it advances. Reusing an exhausted iterator yields no new items under the normal iterator protocol.

    Undergraduate
  29. Scanning from the Front or Back

    Traversal direction determines which qualifying item a short-circuit search encounters first.

    Undergraduate
  30. Finding Minimum and Maximum in One Pass

    A single traversal can maintain both extremes, which is useful for inputs that can be visited only once.

    Undergraduate