One-Line FizzBuzz Solution in Python 3
Introduction
The FizzBuzz problem is a common problem you may encounter during job interviews, and I have given it to potential candidates myself during job interviews. In this article, I present a one-line solution to the problem in Python. The one-line solution is one of the solutions I gave to my coworkers when presented with the FizzBuzz problem about six years ago. The one-line solution is an excellent example of the inline functionalities possible in Python, and I will break down the one-line solution step by step. First, I will explain the problem. In addition to the one-line solution, I will present an alternate solution. Although the alternate explanation is not one line, it is more readable for comparison purposes.
The FizzBuzz Problem
The problem solved in this article is the following. For the integers 1 through 100, print one of the following on each line. For integers divisible by 3, print the word “fizz.” For integers divisible by 5, print the word “buzz.” For integers divisible by both 3 and 5, print the word “fizzbuzz.” For all others, print the actual integer.
Example Printout
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
…
98
fizz
buzz
Multiple Line Solution
Before I explain the one-line solution, I want to present a multiple-line solution to the FizzBuzz problem. The solution below is a more readable solution to the FizzBuzz problem.
for x in range(1, 101):
if x%15 == 0:
print('fizzbuzz')
elif x%5 == 0:
print('buzz')
elif x%3 == 0:
print('fizz')
else:
print(x)
One line FizzBuzz Example
The code line below will print the FizzBuzz values for 1 through 100. Copy and paste the line below in a Python 3 interpreter to see the solution. I will break down the components in the line of code.
print('\n'.join(['fizzbuzz' if x%15 == 0 else 'buzz' if x%5 == 0 else 'fizz' if x%3 == 0 else str(x) for x in range(1,101)]))
In my solution, I start with Python list comprehensions. The line below will create a list of integers from 1 to 100.
[x for x in range(1, 101)]
Now that we have the list from 1 to 100, swap the values divisible by both 3 and 5 with “fizzbuzz.” Add a conditional expression to swap the values divisible by both 3 and 5 with “fizzbuzz.”
['fizzbuzz' if x%15==0 else x for x in range(1, 101)]
Continue to swap values divisible by 3 with “fizz” and values divisible by 5 with “buzz” by adding additional “else” combined with “if” conditional expressions.
['fizzbuzz' if x%15==0 else "buzz" if x%5 == 0 else "fizz" if x%3 == 0 else x for x in range(1, 101)]
Now we have a list of correct values to solve the problem. However, the list is of mixed type “str” and “int” type values. For consistency, wrap the final “else x” value in “str” to make sure all the values in the generated list are of the “str” type.
['fizzbuzz' if x%15==0 else "buzz" if x%5 == 0 else "fizz" if x%3 == 0 else str(x) for x in range(1, 101)]
Each value in the list must be on one line, and the “\n” end of line character will do that. Using the “join” string operator method on “\n” assures each list value is on only one line.
'\n'.join(['fizzbuzz' if x%15==0 else "buzz" if x%5 == 0 else "fizz" if x%3 == 0 else str(x) for x in range(1, 101)])
Finally, wrap the print function around the line above and see the solution in the interpreter.
print('\n'.join(['fizzbuzz' if x%15==0 else "buzz" if x%5 == 0 else "fizz" if x%3 == 0 else str(x) for x in range(1, 101)]))
Conclusion
The next time an interviewer asks this question of you, present this solution. The one-line solution is a good learning exercise for the cool inline stuff you can do in Python 3.