Classic FizzBuzz
The classic FizzBuzz problem description is as follows:
"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”."
The solution to the problem has been widely discussed. One possible solution is the following:
for i in range(0,101):
s = ''
if i % 3 == 0:
s += 'Fizz'
if i % 5 == 0:
s += 'Buzz'
print(s) if s else i
Changing the description
To solve the problem a more flexible description may be considered:
"Write a program that generates the numbers from 1 to n.
But for multiples of three gives “Fizz” instead of the
number and for the multiples of five gives “Buzz”. For
numbers which are multiples of both three and five gives
“FizzBuzz”."
This, in fact, leads to a more flexible solution. To print the results one just needs to use a generator
:
>>> from fizzbuzz_lib import classic_fizzbuzz
>>> for i in classic_fizzbuzz(10):
... print(i)
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
This allows to store the solution in a container or to use it in a more lazy manner. For example:
>>> [s for s in classic_fizzbuzz(10) if s != 'Buzz']
['1', '2', 'Fizz', '4', 'Fizz', '7', '8', 'Fizz']
In fizzbuzz_lib
you may find some other useful functions.
CLI script
A CLI script 1 fizzbuzz_cli.py
is available to execute this function from console 2:
fizzbuzz $> ./fizzbuzz_cli.py 10
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz
See fizzbuzz_cli.py --help
for other options.
Web services
Web services 3 may also be used to generate the FizzBuzz sequence. Just execute run_fastapi_server.sh
and then open in a browser http://localhost:8000/docs. A description of the available services will appear. You may also use http://localhost:8000/redoc to see the documentation in an alternative scheme.
The CLI script has also options to act as a client to consume the web services. See fizzbuzz_cli.py cfbw --help
for more details.
Footnotes