Hone logo
Hone
Problems

Infinite Sequence Generator

Generators in Python provide a powerful way to create iterators without storing the entire sequence in memory. This is particularly useful when dealing with potentially infinite sequences or very large datasets. This challenge will test your understanding of generator functions and how to use them to efficiently produce sequences.

Problem Description

You are tasked with creating a generator function called infinite_sequence that yields an infinite sequence of numbers based on a given starting value and a step size. The generator should start with the given starting value and then repeatedly add the step size to the previous value, yielding each new value. The generator should continue indefinitely, so it should not terminate.

Key Requirements:

  • The generator function must accept two arguments: start (the initial value) and step (the increment).
  • The generator must yield the start value first.
  • Subsequent values yielded must be calculated by adding the step to the previous yielded value.
  • The generator must run indefinitely (no explicit termination condition).

Expected Behavior:

When the generator is iterated over (e.g., using a for loop or next()), it should produce an infinite stream of numbers. The first number yielded will be start, the second will be start + step, the third will be start + 2 * step, and so on.

Edge Cases to Consider:

  • start and step can be any numeric type (int, float).
  • step can be positive, negative, or zero. A zero step will result in an infinite sequence of the same starting value.
  • The generator should handle large values of start and step without causing overflow errors (within the limits of Python's numeric types).

Examples

Example 1:

Input: start = 1, step = 2
Output: 1, 3, 5, 7, 9, ... (infinitely)
Explanation: The generator starts at 1 and adds 2 in each subsequent yield.

Example 2:

Input: start = 10, step = -1
Output: 10, 9, 8, 7, 6, ... (infinitely)
Explanation: The generator starts at 10 and subtracts 1 in each subsequent yield.

Example 3:

Input: start = 5, step = 0
Output: 5, 5, 5, 5, 5, ... (infinitely)
Explanation: The generator starts at 5 and yields 5 indefinitely because the step is 0.

Constraints

  • start and step must be numeric types (int or float).
  • The generator function must be defined using the yield keyword.
  • The generator must run indefinitely. Do not include any explicit termination conditions (e.g., break or return).
  • The generator should be efficient in terms of memory usage. It should not store the entire sequence in memory.

Notes

Consider using a while True loop within the generator function to create an infinite sequence. The yield keyword is crucial for defining a generator. Remember that generators produce values on demand, which is what makes them memory-efficient for infinite sequences. Think about how to keep track of the previous value to calculate the next one.

Loading editor...
python