r/Python • • Jan 23 '17

Share your unusual fizzbuzz approaches!

Dear /r/Python, I would like you to share your FizzBuzz. Here is mine:

import numpy as np

fb = np.arange(101, dtype='object')
fb[::3] = 'Fizz'
fb[::5] = 'Buzz'
fb[::15] = 'FizzBuzz'

print(*fb[1:], sep='\n')
4 Upvotes

20 comments sorted by

View all comments

Show parent comment

1

u/lengau Jan 25 '17

Another really bad way to do it, this time entirely defeating the purpose of a generator by being eager evaluating anyway:

def fizzbuzzer(n):
    fizz = set()
    buzz = set()
    for i in range(1, n+1):
        if not i % 3:
            fizz.add(i)
        if not i % 5:
            buzz.add(i)
    for i in range(1, n+1):
        yield ''.join(('Fizz' if i in fizz else '', 'Buzz' if i in buzz else '', str(i) if i not in fizz|buzz else ''))
for n in fizzbuzzer(100):
    print(n)