Write a program that outputs the string representation of numbers from 1 to n.
For each multiple of 3, print 'Fizz' instead of the number.
For each multiple of 5, print 'Buzz' instead of the number.
For numbers which are multiples of both 3 and 5, print 'FizzBuzz' instead of the number.
n = 15,
Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
class Solution:
def fizzy(self, n):
list = []
for i in range(1, n + 1):
if i % 15 == 0:
list.append('FizzBuzz')
elif i % 3 == 0:
list.append('Fizz')
elif i % 5 == 0:
list.append('Buzz')
else:
list.append(str(i))
return '\n'.join(list)
- The modulo operator finds the remainder after the division of one number by another
- Create a list that will store the output of numbers 1 to n, then create a loop to iterate over those numbers
list = [] for i in range(1, n + 1):
- If the number is divisible by both 3 and 5, add 'FizzBuzz' to the list
if i % 15 == 0: list.append('FizzBuzz')
- If the number is divisible by 3, add 'Fizz' to the list
elif i % 3 == 0: list.append('Fizz')
- If the number is divisible by 5, add 'Buzz' to the list
elif i % 5 == 0: list.append('Buzz')
- If the number is not divisible by 3 and 5, add the number to the list
else: list.append(str(i))
- Return the list
return '\n'.join(list)