Fizz Buzz Iterables Assignment

For this assignment you will create two different iterables in Python - one using a class, and another using a generator. Both iterables will generate the sequence used in the game Fizz Buzz.

You will want to consult the in_class-iterables repository which contains two examples that illustrate how to make iterables using a class and a generator.

Here are some additional resources regarding iterators, iterables, and generators:

Fizz Buzz

In the game of Fizz Buzz, players take turns counting incrementally starting with 1. If a player’s number is a multiple of 3, they must say “fizz” instead of their number. If a player’s number is a multiple of 5, they must say “buzz”. If the player’s number is both a multiple of 3 and 5, they must say “fizzbuzz”.

For example, here are the first 15 turns of Fizz Buzz:

1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz

Iterable Behavior

Your iterables must produce an infinite sequence of tuples. The first element of the tuple must be the integer of the next turn of the game, and the second element must be a string representing what the player should say. For example, here are the first 5 tuples that your iterables must generate:

(1, '1')
(2, '2')
(3, 'fizz')
(4, '4')
(5, 'buzz')

You can convert an integer x to a string with str(x). You can return a tuple by returning multiple values separated by commas. For example, if the tuple you’re returning should consist of next_integer and next_string, then you would return them as a tuple like this:

return next_integer, next_string

For the generator, you will do the same thing except using yield in place of return.

Another thing to note about the generator is that the action should be placed in an infinite loop. Since execution only continues when the generator object is asked for the next item, this loop will not truly run infinitely unless there is another infinite loop that is infinitely retrieving the next item.

To create an infinite loop in Python, you can use while True:

while True:
    # compute and yield the next tuple in the sequence

Implementation

There are stubs for the class FizzBuzz and the generator function fizzbuzz_generator() in fizzbuzz.py. I recommend getting one working before moving on to the other. A FizzBuzz object and a generator object returned from fizzbuzz_generator() should have the exact same behavior, so all you have to do to switch from using one to the other is switch which of the first two lines of code in main() are commented out. If things are working correctly, the first 30 tuples from the generator should be printed out by the loop in main().

Since you are creating infinite sequences, there is no need to raise StopIteration in your class nor in your generator. The git-keeper tests will check that your iterables work for more than just the first 30 numbers.

Submission

Push your submission to git-keeper. For full credit all tests must pass, and fizzbuzz_generator() must be a generator function that uses yield.