Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Repeat Until Condition Met

Iterate through a sequence and collect items until a cumulative condition is satisfied.

Python practice6 minLoops & IterationBeginnerLast updated March 15, 2026

Problem statement

Given a list of numbers and a threshold value, return a list containing the prefix of numbers taken from the start of the list up to and including the element that makes the cumulative sum reach or exceed the threshold. If the threshold is less than or equal to zero, return an empty list. If the threshold is never reached, return the entire input list.

Task

Use a loop to accumulate values and stop when a condition (threshold) is met.

Examples

Basic threshold stop

Input

take_until([1, 2, 3, 4], 5)

Output

[1, 2, 3]

Cumulative sums: 1, 3, 6. 6 >= 5 at the third element, so return [1, 2, 3].

Input format

A list of integers (nums) and an integer (threshold).

Output format

A list of integers representing the prefix up to the threshold condition.

Constraints

Do not use external libraries. Preserve original order. Time complexity should be O(n).

Samples

Sample 1

Input

take_until([10, -1, 5], 8)

Output

[10]

10 >= 8 immediately, so only the first element is returned.