9. Fizz Buzz Questions
You are given an integer n . Each number from 1 to n is printed according to the following rules:
- If the number is divisible by 3, print
fizz
. - If the number is divisible by 5, print
buzz
. - If the number is divisible by both
3
and5
, printfizz buzz
.
Sample
For example n = 15
, returns an array of strings:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
class Solution: """ @param n: An integer @return: A list of strings. """ def fizzBuzz(self, n): # write your code here result = [] for i in range(1,n+1): if i==0: result.append("fizz buzz") elif i% 3 == 0: result.append("fizz") elif i% 5 == 0: result.append("buzz") else: result.append(str(i)) return result