Solving Generator Problems

Learn to solve generator problems, starting with a small utility for generating linearly spaced sequences between a start and stop value and examining different techniques.

Generator problems

We will now present a small problem and we will try to solve it using different techniques for the purpose of understanding which programming idioms we can potentially replace with generators. We will write a small utility for generating linearly spaced sequences between a start value and a stop value.

If you have been using MATLAB/Octave or Python NumPy, you might recognize this way of generating evenly (linearly) spaced numbers using a function called linspace(). It’s a handy utility that can be used in various contexts with arbitrary ranges.

We will call our generator lin_space(). Here is a usage example of generating five equally spaced values between 2.0 and 3.0:

Get hands-on with 1400+ tech skills courses.