https://leetcode.com/problems/fizz-buzz/
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
- code
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
# list comprehension with string rule of FizzBuzz
list_of_output = [ 'Fizz' * (not i % 3) + 'Buzz' * (not i % 5 ) or str(i) for i in range(1, n+1) ]
return list_of_output
- code
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(1, n+1):
if i % 15 == 0:
res.append("FizzBuzz")
elif i % 5 == 0:
res.append("Buzz")
elif i % 3 == 0:
res.append("Fizz")
else:
res.append(str(i))
return res