DEV Community

Cover image for fizz buzz implementation
Leandre
Leandre

Posted on

fizz buzz implementation

Introduction

We are going to code the famous fizz buzz game. An easy coding interview question that is often used by companies to see if a programmer they want to hire really knows how to code.
It is very famous and we felt like we should consider working on it and trying different approaches to it.
Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by 3 with the word "fizz", any number divisible by 5 with the word "buzz", and any number divisible by 3 and 5.

Example: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, ...

Implementation

Let's first try the naive approach:
`
def fizzbuzz_naive_approach():

for i in range(1, 101):

    if i%3 == 0:
        print('fizz')

    if i%5 == 0:
        print('buzz')

    if i%3 and i%5 != 0:
        print(i)

return 0
Enter fullscreen mode Exit fullscreen mode

`

The problem with this approach is that for multiples of both 3 and 5 it prints fizz on one row and buzz on the row below.

Let's try to improve this:
`
def fizzbuzz_naive_approach2():

for i in range(1, 101):

    if i%3 == 0 and i%5 != 0:
        print('fizz')

    if i%5 == 0 and i%3 != 0:
        print('buzz')

    if i%3 == 0 and i%5 == 0:
        print('fizzbuzz')

    if i%3 != 0 and i%5 != 0:
        print(i)

return 0
Enter fullscreen mode Exit fullscreen mode

`

The naive approach is improved but still is difficult to read and maintain.

We are going to try and improve the codes implemented above:
`
def fizzbuzz_approach3():

for i in range(1, 101):

    if i%3 == 0 and i%5 == 0:
        print('fizzbuzz')

    elif i%3 == 0:
        print('fizz')

    elif i%5 == 0:
        print('buzz')

    else:
        print(i)

return 0
Enter fullscreen mode Exit fullscreen mode

`

The implementation above is working, but still has more room for improvement. Wondering how to improve this if it is working?

`
def fizzbuzz_approach4():

for i in range(1 ,101):
    output = ""
    if i%3==0: output += "fizz"
    if i%5==0: output += "buzz"

    if output == "": output = i

    print(output)

return 0
Enter fullscreen mode Exit fullscreen mode

`

Conclusion

We are now satisfied with the current approach.
Thank you for walking through the different fizz buzz approaches

Bonus

We created a dictionary to help us loop through any multiple we might be interested in, and the good thing about this implementation is that you do not have to change any code in case you add more multiples to the game. You just have to add the dictionary and you are done with any additional multiple.
`
def fizzbuzz_approach5():

multiples_dict = {3:"fizz", 5:"buzz", 7:"bazz"}

for i in range(1 ,101):
    output = ""
    for j in multiples_dict:
        if i%j==0: output += multiples_dict[j]

    if output == "": output = i

    print(output)

return 0
Enter fullscreen mode Exit fullscreen mode

`
Remember there is always room to improve your code
Thank you for reading the blog post. Subscribe for more articles
Link of the code
Credit to

adeogratias image

Top comments (0)