[Practices Generators]: Looking for Suggestions

For anyone who has some experience with most of the exercises on the Python track, any help/thoughts you can lend below would be very much appreciated. :smile:

Received the following issue today around the Generators Concept exercise, and they do have a point.

While generators and/or generator expressions can be used in the Scale Generator exercise (yes I know it’s deprecated on many other tracks, but please leave that aside for now) — it doesn’t exactly practice generators/generator expressions.

What would be your recommendations for practice exercises that better practice/use generators/generator expressions?

I’m looking for 3-5 (if possible) in increasing level of difficulty. Totally fine to say “none - you’ll need to make them” (and if you have exercise ideas, don’t be shy!!). :smile: Also totally fine to recommend exercises that could be adapted for a <Exercise> Generator Edition version - I’m not opposed to doing a set of Python-specific practice exercise versions if they then help students better practice certain concepts.

These exercises to practice generators should seem somewhat natural – whether because a generator would be more performant/clearer in the solution, or some other important point.

Or to reframe a bit: the exercises would help students understand/answer the question “why use a generator?”. They do not have to require that a generator be returned — just that generators are an important part of the solution.

Again - many thanks in advance for your suggestions! :blue_heart:

1 Like
  • Generators are useful to anything that needs to generate an unbounded sequence – Fibonacci and primes come to mind.
  • Bottle Song where the end count is not specified?
  • Exercises like Change, Largest Series, Pythagorean Triplets can use generators as intermediate values to loop over without storing it all in memory.
  • If you feed an infinite generator into, say, ETL, Scrabble Score, Run Length Encoding, you can force the user to return a generator, from which you can read n elements.
  • Generators are an efficient way to return linked list data without pre-walking the whole list. Though this may require the list not change while the generator is in use.
2 Likes

In the Python track. I used generators in:

  • nth-prime:

        p = prime_generator()
        i = 0
        while i < n:
            prime = next(p)
            i += 1
        return prime
    
  • connect, to iterate over a position’s neighbours:

    for rr, cc in self.neighbours(player, board, r, c):
    
  • binary-search-tree

        def sorted_data(self):
            return [item for item in self.root.inorder()]
    
  • simple-linked list, to somewhat over-engineer the reverse() function:

    class LinkedList(object):
        # ...
        def each(self):
            node = self._head
            while node:
                yield node
                node = node.next()
    
        def reduce(self, func, acc):
            for node in self.each():
                acc = func(acc, node)
            return acc
    
        def reversed(self):
            return self.reduce(
                lambda rev, node: rev.push(node.value()),
                LinkedList()
            )
    

I’ve used coroutines in other tracks for

1 Like

Maybe you can also use generators in collatz-conjecture? I’ve done something similar in other languages, where I’m using a “generator” to generate the sequence and then counting that to get the number of steps.

2 Likes