# Python Challenge - FizzBuzz, FizzBuzz

def _convert(n): if n % 15 == 0: return "FizzBuzz" elif n % 3 == 0: return "Fizz" elif n % 5 == 0: return "Buzz" else: return n def fizzbuzz(limit): # Write your code here return [_convert(i+1) for i in range(limit)] 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):
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): arr = list() for num in range(1, limit+1): if (num % 5) == 0 and (num % 3) == 0: arr.append('FizzBuzz') elif (num % 5) == 0: arr.append('Buzz') elif (num % 3) == 0: arr.append('Fizz') else: arr.append(num) return arr print(fizzbuzz(80))
``````def fizzbuzz(limit):
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): result = list() for i in range(1, limit+1): if i%3 == 0 and i%5 !=0: result.append("Fizz") elif i%5 == 0 and i%3 != 0: result.append("Buzz") elif i%3 ==0 and i%5 == 0: result.append("FizzBuzz") else: result.append(i) return(result) print(fizzbuzz(16))
def fizzbuzz(limit): x = [] for i in range(1,limit+1): if i % 3 == 0 and i % 5 == 0: x.append("FizzBuzz") elif i % 3 == 0: x.append("Fizz") elif i % 5 == 0: x.append("Buzz") else: x.append(i) return x print(fizzbuzz(16))

def fizzbuzz(limit):

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): list = [] for i in range(1, limit + 1): if i%3 == 0 and i%5==0: list.append("FizzBuzz") elif i%5 == 0: list.append("Buzz") elif i%3 == 0: list.append("Fizz") else: list.append(i) return list print(fizzbuzz(16))

def fizzbuzz(limit):

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))

def fizzbuzz(limit): # Write your code here test = list(range(1, limit +1)) fizzbuzzed = [] for i in test: if (i % 3 == 0) and (i % 5 == 0): fizzbuzzed.append('FizzBuzz') elif i % 3 == 0: fizzbuzzed.append('Fizz') elif i % 5 == 0: fizzbuzzed.append('Buzz') else: fizzbuzzed.append(i) return fizzbuzzed 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’!

def fizzbuzz(n): new_list = [] for k in range(1, n+1): if k % 3 == 0 and k % 5 != 0: new_list.append("Fizz") elif k % 5 == 0 and k % 3 != 0: new_list.append('Buzz') elif k % 5 == 0 and k % 3 == 0: new_list.append("FizzBuzz") elif k % 5 != 0 and k % 3 !=0: new_list.append(k) return new_list print(fizzbuzz(15))
def fizzbuzz(limit): # Write your code here numbers = list(range(1, limit+1)) solution = [] for number in numbers: if number % 3 == 0 and number % 5 == 0: solution.append('FizzBuzz') elif number % 3 == 0: solution.append('Fizz') elif number % 5 == 0: solution.append('Buzz') else: solution.append(number) return solution print(fizzbuzz(16))

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):
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))

``````
def fizzbuzz(limit): new_list = [i for i in range(1, limit + 1)] for i in range(len(new_list)): if new_list[i] % 3 == 0 and new_list[i] % 5 == 0: new_list[i] = 'FizzBuzz' elif new_list[i] % 3 == 0: new_list[i] = 'Fizz' elif new_list[i] % 5 == 0: new_list[i] = 'Buzz' return new_list 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.