Skip to content

Instantly share code, notes, and snippets.

@greut
Last active March 30, 2016 14:54
Show Gist options
  • Save greut/e0cce5a2518b900f35479172336ed528 to your computer and use it in GitHub Desktop.
Save greut/e0cce5a2518b900f35479172336ed528 to your computer and use it in GitHub Desktop.
Coroutine python fizzbuzz
"""FizzBuzz.
Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz” instead of the number and
for the multiples of five print “Buzz”.
For numbers which are multiples of both three and five print “FizzBuzz”.
-- http://c2.com/cgi/wiki?FizzBuzzTest
"""
def fizzy(number, text, destination):
next(destination)
while True:
value = yield
if isinstance(value, int) and value % number == 0:
destination.send(text)
else:
destination.send(value)
def printer():
while True:
print((yield))
pipeline = fizzy(15, "FizzBuzz",
fizzy(5, "Buzz",
fizzy(3, "Fizz", printer())))
next(pipeline)
for i in range(1, 101):
pipeline.send(i)
pipeline.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment