Fizz Buzz in Python

Fizz Buzz in Python

Image for post

Solving the preeminent code interview question.

Image for post

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 36 is a multiple of 39 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): if 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:89 is a multiple of 310 is a multiple of 51112 is a multiple of 3131415 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:1415 is Multiple of 3 AND 516…4445 is Multiple of 3 AND 546…

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 5161718 is a multiple of 31920 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:…FizzBuzz11Fizz1314FizzBuzz16…

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 test- Where did it come from ?A children’s game: fizz buzz game

Thanks for reading !

Keno

13

No Responses

Write a response