# Fizz Buzz in Python

## Solving the preeminent code interview question.

It is said that the fizz buzz question/coding challenge can filter many prospective candidates from a job interview, and as such it has become expected while interviewing, I hope you will get a good understanding along with solutions in python after reading this short post.

➤ Side Note:I have always had a mix feeling about coding interviews for filtering out candidates and how effective they really are, you can read more about my reasoning here :Hire me notBut I am also not kidding myself, coding interviews, challenges, white boarding and leet code are the norm these days...And since a lot of good folks learning a language are also interested in getting a coding job, I feel I need to write more about this aspect.Onwards then !

# The Fizz Buzz Coding Challenge :

“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”.”

I usually start by writing down or sketching the problem since I am a very visual person, but if you think more in your head your are good to go, here’s what the output should look like according to my sketch:

So the first thing we are going to need is 100 numbers:

for i in range(100):

print(i+1)# (i+1) Should print 1 to 100 inclusive (i) by itself would print from 0 to 99. # Perhaps better, you could also write it as range (1,101) and avoid adding the 1, so that's what we will use:for i in range(1,100):

print(i)

All right then, next we need to figure out how to write **fizz** when the number `i`

is a multiple of `3`

:

for i in range(1, 101):

if i% 3 == 0:

print ( str(i) + ' is multiple of 3')# OUTPUT:

3 is a multiple of 3

6 is a multiple of 3

9 is a multiple of 3

.

.

99 is a multiple of 3

**⚠️ You might have noticed that I am not using the words fizz and buzz yet, this is intentional, we want to make things as explicit and clear as possible while we are investigating solutions.**

The trick here is to use the modulo operator which **according to the documentation** :

The % (modulo) operator yields the remainder from the division of the first argument by the second.So 3 % 3 is equal to zero and 4 % 3 is not (it yields 1)

Armed with this knowledge, we can now write a conditional to differentiate between multiples of 3 and multiples of 5 :

for i in range(1, 100):

ifi % 3 == 0:

print ( str(i) + ' is a multiple of 3')

elifi % 5 == 0:

print ( str(i) + ' is a multiple of 5')

else:

print ( str(i) )# PARTIAL OUTPUT:8

9 is a multiple of 3

10 is a multiple of 5

11

12 is a multiple of 3

13

14

15 is a multiple of 3

The only thing left to do is to catch the situation where `i`

is multiple of both 3 and 5 :

for i in range(1, 100):

if i % 3 == 0 and i % 5 == 0:

print ( str(i) + ' is Multiple of 3 AND 5')

else:

print ( str(i) )# PARTIAL OUTPUT:14

15 is Multiple of 3 AND 5

16...

44

45 is Multiple of 3 AND 5

46...

So we seem to have everything we need, the last thing to do is check for all the cases ( 4 if you count them ) and finally replace our explicit readouts with fizz,buzz and fizzbuzz:

for i in range(1, 101):

if i % 3 == 0 and i % 5 == 0:

print ( str(i) + ' is Multiple of 3 AND 5')elif i % 3 == 0:

print ( str(i) + ' is a multiple of 3')elif i % 5 == 0:

print ( str(i) + ' is a multiple of 5')else:

print ( str(i) )# PARTIAL OUTPUT:15 is Multiple of 3 AND 5

16

17

18 is a multiple of 3

19

20 is a multiple of 5...Note that you could also try for a modulo of 15 test since this is the earliest number divisible by 3 and 5:if i % 15== 0:

⚠️ Note where the conditional for both cases is placed ( **First** **if **), if you were to place it somewhere else it wouldn’t work because one of the other conditionals would trigger first, that’s also why we are still printing explicit readouts, it is easier to catch mistakes.

And finally:

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 (str(i))# PARTIAL OUTPUT:...

Fizz

Buzz

11

Fizz

13

14

FizzBuzz

16...

## Variations:

Of course this being programming, there are usually more ways to both state the problem and solve it, you could be asked to write only fizz, buzz or be presented with the same problem in a different context.

As for the solution, there is as much variety as there are coding styles, here’s a few I found online :

for i in range(1,101):

fizz = 'Fizz' if i%3==0 else ''

buzz = 'Buzz' if i%5==0 else ''

print(f'{fizz}{buzz}' or i)

Here the conditionals and the print statement work in conjunction to output the correct solution, note there also a different/newer syntax for printing.

Even shorter:

print(*map(lambda i: 'Fizz'*(not i%3)+'Buzz'*(not i%5) or i, range(1,101)),sep='\n')

Using map and lambdas, not sure I would recommend it, while it is shorter it might come across as difficult to read and the submitter as a show off, so I'd stick to the first or second.

## And that’s it.

You should now know enough about the fizz buzz coding challenge and how to solve it, if you are coming from another language you will additionally need to modify the syntax, but the logic would remain the same.

## End Notes:

- Why is it hard ?I think it is because it requires you to understand a problem, apply logic to come up with a solution and then translate this into code, which if you think about it is 50% of what a developer does ( the other 50% is usually meetings and emails).For more read here:The fizz buzz testA children's game:

- Where did it come from ?fizz buzz game

Thanks for reading !

Keno