def fizzbuzz(limit):
list_num =
i = 0
while limit > i:
i = i + 1
if i % 3 == 0 and i % 5==0:
list_num.append(“FizzBuzz”)
elif i % 3 == 0:
list_num.append(“Fizz” )
elif i % 5 == 0:
list_num.append(“Buzz” )
else:
list_num.append(i)
return list_num
print(fizzbuzz(16))
def fizzbuzz(limit):
list_num =
i = 0
while limit > i:
i = i + 1
if i % 3 == 0 and i % 5==0:
list_num.append(“FizzBuzz”)
elif i % 3 == 0:
list_num.append(“Fizz” )
elif i % 5 == 0:
list_num.append(“Buzz” )
else:
list_num.append(i)
return list_num
print(fizzbuzz(16))
def fizzbuzz(limit):
# Write your code here
list = []
for i in range(1, limit + 1):
list.append('FizzBuzz' if i % 3 == 0 and i % 5 == 0 else 'Fizz' if i % 3 == 0 else 'Buzz' if i % 5 == 0 else i)
return list
print(fizzbuzz(16))
def fizzbuzz(limit):
oneToNList = []
for i in range(1,limit+1):
if (i%3==0) and (i%5 ==0):
oneToNList.append('FizzBuzz')
continue
if i%3==0:
oneToNList.append('Fizz')
continue
if i%5==0:
oneToNList.append('Buzz')
continue
else:
oneToNList.append(i)
return oneToNList
print(fizzbuzz(16))
Hopefully the wording is correct
def fizzbuzz(limit):
Write your code here
k =
for i in range(1,limit+1):
if(i%3==0 and i%5==0):
k.append(“FizzBuzz”)
if(i%3==0 and i%5!=0):
k.append("Fizz")
if(i%3!=0 and i%5==0):
k.append("Buzz")
if(i%3!=0 and i%5!=0):
k.append(i)
return k
print(fizzbuzz(16))
def fizzbuzz(limit):
number_list = list(range(1, limit + 1))
for i in range(limit):
if (number_list[i] % 3) == 0 and (number_list[i] % 5) != 0:
number_list[i] = ‘Fizz’
elif (number_list[i] % 3) != 0 and (number_list[i] % 5) == 0:
number_list[i] = ‘Buzz’
elif (number_list[i] % 3) == 0 and (number_list[i] % 5) == 0:
number_list[i] = ‘FizzBuzz’
return number_list
def fizzbuzz(limit):
Write your code here
n = 0
n_list = [ ]
fb_list = [ ]
while n != limit:
n = n + 1
n_list.append(n)
for i in n_list:
if i % 3 == 0 and i % 5 == 0:
fb_list.append(‘FizzBuzz’)
elif i % 3 == 0:
fb_list.append(‘Fizz’)
elif i % 5 == 0:
fb_list.append(‘Buzz’)
else:
fb_list.append(i)
return fb_list
print(fizzbuzz(16))
def fizzbuzz(limit):
return[“FizzBuzz” if i%3 == 0 and i%5 == 0 else “Fizz” if i%3 == 0 else “Buzz” if i%5 == 0 else i for i in range(1, limit+1)]
print(fizzbuzz(16))
Looking through this thread it seems a lot of people have done it in a similar manner to myself. I have seen quite a few people write it in a more concise manner. I think that I will have to give that a practice next.
I felt very good about myself having read that this sort of challenge has been used in skills tests on interviews but then I soon came crashing down to Earth, upon reading that some people think it should be labelled ‘easy’!
fizzbuzz = lambda limit: [‘FizzBuzz’ if (i%5==0 and i%3==0) else ‘Fizz’ if i%3==0 else ‘Buzz’ if i%5==0 else i for i in range(1,limit+1)]
print(fizzbuzz(16))
I did the same thing
def fizzbuzz(n):
result = ['FizzBuzz' if i % 15 == 0 else 'Fizz' if i % 3 == 0 else 'Buzz' if i % 5 == 0 else i for i in range(1, n+1)]
return result
# Test the function
print(fizzbuzz(16))
What is with the fifteen?
def fizzbuzz(limit):
# Write your code here
fizzbuzz = []
for num in range(1, limit + 1):
if (num % 3 == 0) and (num % 5 == 0):
fizzbuzz.append("FizzBuzz")
elif num % 3 == 0:
fizzbuzz.append("Fizz")
elif num % 5 == 0:
fizzbuzz.append("Buzz")
else:
fizzbuzz.append(num)
return fizzbuzz
print(fizzbuzz(16))
I am learning Python coming from C++ and Java. No idea how decent or “Pythonic” this code is, just know that it works. I created a new list using comprehension and iterated over it, reassigning values at the indices that met the conditions. I wanted to do it all in one pass, but I am not that experienced with Python yet.