FizzBuzz Problem: Examining Pythonic Solutions

Lukman Aliyu
4 min readApr 22, 2023

--

Figure 1. Pythonic FizzBuzz solutions

The FizzBuzz problem is a popular programming interview question that tests a candidate’s ability to write a simple program that can solve a straightforward problem. The problem involves printing numbers from 1 to 100 while substituting the numbers that are multiples of 3 with the word “Fizz”, multiples of 5 with the word “Buzz”, and multiples of both 3 and 5 with the word “FizzBuzz”.

The problem might seem trivial at first glance, but it is an effective way to weed out candidates who lack basic programming skills. The FizzBuzz problem tests several fundamental programming concepts, including loops, conditionals, and basic arithmetic.

The FizzBuzz problem has several solutions, ranging from simple to complex. The most straightforward solution involves using a loop to iterate through the numbers from 1 to 100 and using an if statement to check if the current number is divisible by 3, 5, or both. If the number is divisible by 3, the program prints “Fizz”. If the number is divisible by 5, the program prints “Buzz”. If the number is divisible by both 3 and 5, the program prints “FizzBuzz”. Otherwise, the program prints the number itself.

  1. Regular solution using loops
def fizzbuzz(n):
for number in range(1,n):
if number % 3 == 0 and number % 5 == 0:
print('FizzBuzz', end = ' ')
elif number % 3 == 0:
print('Fizz',end = ' ')
elif number % 5 == 0:
print('Buzz', end = ' ')
else:
print(number, end = ' ')
return

print(fizzbuzz(101))

The most straightforward solution is the one above and it involves using a loop to iterate through the numbers from 1 to 100 and using an if statement to check if the current number is divisible by 3, 5, or both. If the number is divisible by 3, the program prints “Fizz”. If the number is divisible by 5, the program prints “Buzz”. If the number is divisible by both 3 and 5, the program prints “FizzBuzz”. Otherwise, the program prints the number itself.

2. Lambda function

fizzbuzz = lambda n: 'FizzBuzz' if n % 15 == 0 else 'Fizz' if n % 3 == 0 else 'Buzz' if n % 5 == 0 else str(n)
print([fizzbuzz(i) for i in range(1,101)])

Same as the first solution but this time a lambda function is used to replace the regular function in solution 1 above.

3 . List comprehension

print(['FizzBuzz' if i % 15 == 0 else 'Fizz' if i % 3 == 0 else 'Buzz' if i % 5 == 0 else i for i in range(1,101)])

In the third solution, list comprehension is used which is a very handy way of executing a loop that returns a list. It cuts out the part where the a new list initiated and items are appended to the new list. Very efficient.

4. Ternary operator

print( [('' if n%3 else 'Fizz') + ('' if n%5 else 'Buzz') or n for n in range(1,101)])

In this fourth solution:

(‘’ if n%3 else ‘Fizz’) returns an empty string if n % 3 is not zero (i.e. the number is not divisible by 3) and ‘Fizz’ if the n%3 is zero (i.e. number is divisible by 3). Same explanation applies to (‘’ if n%5 else ‘Buzz’). The plus part will ensure that (‘’ if n%3 else ‘Fizz’) + (‘’ if n%5 else ‘Buzz’) returns ‘FizzBuzz’ if number is divisible by 15, ‘Fizz’ if number is divisible by 3 and ‘Buzz’ if number is divisible by 5. The or part ‘or n for n in range(1,101)’ returns the number. or operator is not applied until the left part is evaluated and is False. Hence, if the first part is ‘Fizz’, ‘Buzz, or ‘FizzBuzz’, the or part will not be evaluated.

The FizzBuzz problem might seem like a simple exercise, but it is an effective way to gauge a programmer’s understanding of basic programming concepts. While experienced programmers might find the problem trivial, it is an excellent tool for identifying candidates who lack basic programming skills.

Conclusion

In conclusion, the FizzBuzz problem is a popular programming interview question that tests a candidate’s ability to write a simple program to solve a straightforward problem. The problem involves printing numbers from 1 to 100 while substituting the numbers that are multiples of 3 with the word “Fizz”, multiples of 5 with the word “Buzz”, and multiples of both 3 and 5 with the word “FizzBuzz”. The problem tests several fundamental programming concepts, including loops, conditionals, and basic arithmetic, and has several solutions, ranging from simple to complex as demonstrated in this article.

--

--

Lukman Aliyu
Lukman Aliyu

Written by Lukman Aliyu

Pharmacist enthusiastic about Data Science/AI/ML| Fellow, Arewa Data Science Academy

No responses yet