Fizz Buzz in Python

Solving the preeminent code interview question.

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

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)
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
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)
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: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
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...
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:
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:

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

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

AI, Software Developer, Designer : www.k3no.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store