Skip to content

Instantly share code, notes, and snippets.

@dckc
Last active December 18, 2019 12:01
Show Gist options
  • Save dckc/d58a04b154d44d46de9abcda84b2a2f0 to your computer and use it in GitHub Desktop.
Save dckc/d58a04b154d44d46de9abcda84b2a2f0 to your computer and use it in GitHub Desktop.
"""Simple fizzbuzz generator.
The game proceeds with players announcing numbers increasing
sequentially, except for multiples of 3 and 5:
>>> usage = 'fizzbuzz 1 20'
>>> from io import StringIO; stdout = StringIO()
>>> main(usage.split(), stdout)
>>> print(stdout.getvalue())
... #doctest: +ELLIPSIS
1
2
fizz
4
buzz
fizz
7
...
14
fizzbuzz
16
...
"""
def main(argv, stdout):
[lo_, hi_] = argv[1:3]
lo, hi = int(lo_), int(hi_)
for word in fizzbuzz(lo, hi):
print(word, file=stdout)
def fizzbuzz(lo, hi):
"""
>>> list(fizzbuzz(1, 6))
[1, 2, 'fizz', 4, 'buzz']
"""
for n in range(lo, hi):
if n % 3 == 0 and n % 5 == 0:
yield "fizzbuzz"
elif n % 3 == 0:
yield "fizz"
elif n % 5 == 0:
yield "buzz"
else:
yield n
if __name__ == '__main__':
def _script_io():
from sys import argv, stdout
main(argv, stdout)
_script_io()
@dckc
Copy link
Author

dckc commented Jul 27, 2019 via email

@fernandezpablo85
Copy link

Yeah, found out that the explanation was in your post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment