In [15]:
#common interview problem:
#input: int n
#output: list of string converted numbers from 1 to n, where numbers are replaced by 'Fizz' if divisible by 3
#'Buzz' if divisible by 5, and 'FizzBuzz' if divisible by both 3 and 5.
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
l = []
for i in range(1,n+1):
if i % 3 == 0:
if i%5 == 0:
l.append('FizzBuzz')
else:
l.append('Fizz')
elif i%5==0:
l.append('Buzz')
else:
l.append(str(i))
return l
In [16]:
n = 15
myobj = Solution()
myobj.fizzBuzz(15)
Out[16]:
In [17]:
Solution().fizzBuzz(15)
Out[17]:
In [ ]: