Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 9

Iterate With Range

Easy

10 minute session

Summary

Generate sequences of integers using range parameters and loops.

Problem statement

Create a function iterate_with_range(start, stop, step=1) that returns a list of integers produced by iterating from start to stop using step, similar to Python's built-in range behavior. If step is zero, return an empty list instead of raising an exception. Use a loop to build the resulting list.

Task

Implement a function that returns the list of integers produced by Python's range(start, stop, step), handling a zero step gracefully.

Examples

Positive step

Input

iterate_with_range(0, 5)

Output

[0, 1, 2, 3, 4]

Explanation

Numbers from 0 up to (but not including) 5 with step 1.

Input format

A function call iterate_with_range(start, stop, step) where start and stop are integers and step is an integer (optional, defaults to 1).

Output format

Return a list of integers generated by stepping from start to stop using step. If step is 0, return an empty list.

Constraints

-10**9 <= start, stop <= 10**9, step is any integer. The resulting list length will be reasonable for typical inputs in exercises.

Samples

Sample input 0

iterate_with_range(5, 0, -2)

Sample output 0

[5, 3, 1]

Explanation 0

Start at 5, step -2, stop before reaching 0.

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:42 AM

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