Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 10

Repeat Until Condition Met

Easy

6 minute session

Summary

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

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]

Explanation

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 input 0

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

Sample output 0

[10]

Explanation 0

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

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:41 AM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.