Skip to main content
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = count(1)
    f = map(print, map(derailleur, counter, carousel))
    while 1:
        next(f)

main()
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = count(1)
    f = map(print, map(derailleur, counter, carousel))
    while 1:
        next(f)

main()
user@Desktop:~$ python3 fizzbuzz.py | pv > /dev/null
^C21MiB 0:00:30 [14,2MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
  File "fizzbuzz.py", line 3, in derailleur
    def derailleur(counter, carousel):
KeyboardInterrupt
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

user@Desktop:~$ pypy3 fizzbuzz.py | pv > /dev/null
^C57MiB 0:00:30 [25,0MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
KeyboardInterrupt
user@Desktop:~$ python3 fizzbuzz.py | pv > /dev/null
^C21MiB 0:00:30 [14,2MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
  File "fizzbuzz.py", line 3, in derailleur
    def derailleur(counter, carousel):
KeyboardInterrupt
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

user@Desktop:~$ pypy3 fizzbuzz.py | pv > /dev/null
^C57MiB 0:00:30 [25,0MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
KeyboardInterrupt
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = map(str, count(1))
    f = map(derailleur, counter, carousel)
    while 1:
        print('\n'.join([next(f) for _ in range(256)]))

main()
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = map(str, count(1))
    f = map(derailleur, counter, carousel)
    while 1:
        print('\n'.join([next(f) for _ in range(256)]))

main()
user@Desktop:~$ python3 chunking_fizzbuzz.py | pv > /dev/null
^C84MiB 0:00:30 [19,6MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt

user@Desktop:~$ pypy3 chunking_fizzbuzz.py | pv > /dev/null
^C49GiB 0:00:30 [84,9MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
  File "chunking_fizzbuzz.py", line 13, in <listcomp>
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt
user@Desktop:~$ python3 chunking_fizzbuzz.py | pv > /dev/null
^C84MiB 0:00:30 [19,6MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt

user@Desktop:~$ pypy3 chunking_fizzbuzz.py | pv > /dev/null
^C49GiB 0:00:30 [84,9MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
  File "chunking_fizzbuzz.py", line 13, in <listcomp>
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = count(1)
    f = map(print, map(derailleur, counter, carousel))
    while 1:
        next(f)

main()
user@Desktop:~$ python3 fizzbuzz.py | pv > /dev/null
^C21MiB 0:00:30 [14,2MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
  File "fizzbuzz.py", line 3, in derailleur
    def derailleur(counter, carousel):
KeyboardInterrupt
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

user@Desktop:~$ pypy3 fizzbuzz.py | pv > /dev/null
^C57MiB 0:00:30 [25,0MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
KeyboardInterrupt
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = map(str, count(1))
    f = map(derailleur, counter, carousel)
    while 1:
        print('\n'.join([next(f) for _ in range(256)]))

main()
user@Desktop:~$ python3 chunking_fizzbuzz.py | pv > /dev/null
^C84MiB 0:00:30 [19,6MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt

user@Desktop:~$ pypy3 chunking_fizzbuzz.py | pv > /dev/null
^C49GiB 0:00:30 [84,9MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
  File "chunking_fizzbuzz.py", line 13, in <listcomp>
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = count(1)
    f = map(print, map(derailleur, counter, carousel))
    while 1:
        next(f)

main()
user@Desktop:~$ python3 fizzbuzz.py | pv > /dev/null
^C21MiB 0:00:30 [14,2MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
  File "fizzbuzz.py", line 3, in derailleur
    def derailleur(counter, carousel):
KeyboardInterrupt
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

user@Desktop:~$ pypy3 fizzbuzz.py | pv > /dev/null
^C57MiB 0:00:30 [25,0MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
KeyboardInterrupt
from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = map(str, count(1))
    f = map(derailleur, counter, carousel)
    while 1:
        print('\n'.join([next(f) for _ in range(256)]))

main()
user@Desktop:~$ python3 chunking_fizzbuzz.py | pv > /dev/null
^C84MiB 0:00:30 [19,6MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt

user@Desktop:~$ pypy3 chunking_fizzbuzz.py | pv > /dev/null
^C49GiB 0:00:30 [84,9MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
  File "chunking_fizzbuzz.py", line 13, in <listcomp>
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt
Source Link
ksousa
  • 91
  • 1
  • 6

Python3

Interesting problem. I see most answers used static languages, except the Powershell answer, so far untimed. As I was curious about how dynamic languages would fare in this task, I wrote a simple implementation in Python.

I ran it under GNU/Linux Mint 20.02 64-bit, using default Python3 (3.8.10) and pypy (7.3.1) in the repositories. Processor model in my machine: AMD Athlon(tm) X4 750 Quad Core.

Initial version

The initial version just keeps a carousel running in sync to a counter, where the carousel carries either False or the results different from the current count, in the proper positions. The derailleur function selects what is to be printed.

from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = count(1)
    f = map(print, map(derailleur, counter, carousel))
    while 1:
        next(f)

main()

In my machine it ran at about 14,2MiB/s under CPython, and got a modest boost up to 25,0MiB/s under pypy:

user@Desktop:~$ python3 fizzbuzz.py | pv > /dev/null
^C21MiB 0:00:30 [14,2MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
  File "fizzbuzz.py", line 3, in derailleur
    def derailleur(counter, carousel):
KeyboardInterrupt
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

user@Desktop:~$ pypy3 fizzbuzz.py | pv > /dev/null
^C57MiB 0:00:30 [25,0MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "fizzbuzz.py", line 15, in <module>
    main()
  File "fizzbuzz.py", line 13, in main
    next(f)
KeyboardInterrupt

Chunking version

Afterwards I modified the initial version to print the results in chunks, instead of one by one.

from itertools import cycle, count

def derailleur(counter, carousel):
    if not carousel:
        return counter
    return carousel

def main():
    carousel = cycle([0,0,'Fizz',0,'Buzz','Fizz',0,0,'Fizz','Buzz',0,'Fizz',0,0,'FizzBuzz'])
    counter = map(str, count(1))
    f = map(derailleur, counter, carousel)
    while 1:
        print('\n'.join([next(f) for _ in range(256)]))

main()

Now under CPython it runs a bit faster, at about 19,6MiB/s. But under pypy it receives a large boost, achieving 84,9MiB/s, within half the speed of the naive implementation written in C reported by the OP (170MiB/s):

user@Desktop:~$ python3 chunking_fizzbuzz.py | pv > /dev/null
^C84MiB 0:00:30 [19,6MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt

user@Desktop:~$ pypy3 chunking_fizzbuzz.py | pv > /dev/null
^C49GiB 0:00:30 [84,9MiB/s] [                            <=>                   ]
Traceback (most recent call last):
  File "chunking_fizzbuzz.py", line 15, in <module>
    main()
  File "chunking_fizzbuzz.py", line 13, in main
    print('\n'.join([next(f) for _ in range(256)]))
  File "chunking_fizzbuzz.py", line 13, in <listcomp>
    print('\n'.join([next(f) for _ in range(256)]))
KeyboardInterrupt

I'm curious to see how much this result can be improved upon, for Python, and how other dynamic languages perform.