Lesson guide
What this Python exercise practices
Repeat Until Condition Met is a beginner practice lesson that focuses on loops, iteration, counters. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Lists or strings
- Basic for loop syntax
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Related public exercises
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.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.