Python Challenge - FizzBuzz, FizzBuzz

def fizzbuzz(limit):
  mylist = []
  for num in range(1,limit + 1):
    if num%3 == 0 and num%5 == 0:
      mylist.append('FizzBuzz')
    elif num%3 == 0:
      mylist.append('Fizz')
    elif num%5 == 0:
      mylist.append('Buzz')
    else:
      mylist.append(num)
  return mylist
  
print(fizzbuzz(16))

def fizzbuzz(limit):
    _list = list(range(1, limit+1))
    results = list(map(convert_numbers, _list))
    return results
   
def convert_numbers(num):
    if num % 3 == 0 and num % 5 == 0:
      return "FizzBuzz"
    if num % 3 == 0:
      return 'Fizz'
    if num % 5 == 0:
      return 'Buzz'
    return num

def fizzbuzz(limit):
  # Write your code here
  arr = []
  for i in range(1,limit+1):
    s = ""
    if i%3==0:
      s += "Fizz"
    if i%5==0:
      s += 'Buzz'
    arr.append(s if s!='' else i)
  return arr

print(fizzbuzz(16))



def fizzbuzz(limit):
 # Write your code here
 return ["Fizz" * (x % 3 == 0) + "Buzz" * (x % 5 == 0) or x for x in range(1, limit+1)]


print(fizzbuzz(16))


def fizzbuzz(limit): # Write your code here return_list = [] new_list = range(1,limit+1) for num in new_list: if num % 3 == 0 and num % 5 == 0: return_list.append('FizzBuzz') elif num % 3 == 0 and num % 5 != 0: return_list.append('Fizz') elif num % 3 != 0 and num %5 == 0: return_list.append('Buzz') else: return_list.append(num) return return_list print(fizzbuzz(16))
def fizzbuzz(limit): list = [] for num in range(0, limit +1): list.append(num) for i in range(len(list)): if i % 3 == 0: list[i] = "Fizz" if i % 5 == 0: list[i] = "Buzz" if i % 3 == 0 and i % 5 == 0: list[i] = "FizzBuzz" list.remove("FizzBuzz") return list print(fizzbuzz(16))

def fizzbuzz(limit):

Write your code here

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):
num = 1
lst =
while num <= limit:
if num % 15 == 0:
lst.append(‘FizzBuzz’)
num += 1
elif num % 5 == 0:
lst.append(‘Buzz’)
num += 1
elif num % 3 == 0:
lst.append(‘Fizz’)
num += 1
else:
lst.append(num)
num += 1
return lst

print(fizzbuzz(16))

def fizzbuzz(limit): lst = [] for n in range(1,limit+1): if n % 3 == 0 and n % 5 == 0: lst.append('FizzBuzz') elif n % 3 == 0: lst.append('Fizz') elif n % 5 == 0: lst.append('Buzz') else: lst.append(n) return lst print(fizzbuzz(16))

This is my code :slight_smile: :
def prefizzbuzz(n):
dict = {
(True, True) : “FizzBuzz”,
(True, False) : “Fizz”,
(False, True) : “Buzz”,
(False, False) : n
}
return dict[(n%3==0, n%5==0)]

def fizzbuzz(limit):
return [prefizzbuzz(integer) for integer in range(1, limit+1)]

print(fizzbuzz(16))

2 Likes
def fizzbuzz(limit): lst = [] for i in range(1,limit+1): if i %3 == 0 and i % 5 != 0: i = "Fizz" elif i%5 == 0 and i % 3 != 0: i = "Buzz" elif i % 3 == 0 and i % 5 ==0: i = "FizzBuzz" lst.append(i) return lst print(fizzbuzz(16))
def fizzbuzz(n): list_to_return = [] for i in range(1,n+1): if i%5 == 0 and i%3 == 0: list_to_return.append("FizzBuzz") elif i%5 == 0: list_to_return.append("Buzz") elif i%3 == 0: list_to_return.append("Fizz") else: list_to_return.append(i) return list_to_return print(fizzbuzz(16))

This was the simplest way I in my mind, but I saved limit as n for preference. Probably not a good idea for longer code but this was pretty short and simple.

def fizzbuzz(limit): n = limit lst = [] for num in range(1, n + 1): if num % 3 == 0 and num % 5 == 0: lst.append('FizzBuzz') elif num % 3 == 0: lst.append('Fizz') elif num % 5 == 0: lst.append('Buzz') else: lst.append(num) return list(lst) print(fizzbuzz(16))
def fizzbuzz(limit): num_list = [*range(1, limit+1)] #print(num_list) def is_multiple(divisor, value): return (value % divisor == 0) fizz = ["Fizz" if is_multiple(3, num) else '' for num in num_list] buzz = ["Buzz" if is_multiple(5, num) else '' for num in num_list] fizzbuzz = [f + b for f,b in zip(fizz,buzz)] fizzbuzz = [label if label else num for label, num in zip(fizzbuzz, num_list)] return fizzbuzz print(fizzbuzz(16))

I went with list comprehension for mine.

def fizzbuzz(limit):
  ans = []
  if limit > 0:
    for i in range(1,limit+1):
      if i%15 == 0:
        ans.append("FizzBuzz")
      elif i%5 == 0:
        ans.append("Buzz")
      elif i%3 == 0:
        ans.append("Fizz")
      else:
        ans.append(i)
  else:
    pass
  return ans
  # Write your code here


print(fizzbuzz(16))

def fizzbuzz(limit): # Write your code here # New empty list fizzbuzz_list = [] number = 1 while number <= limit: if number % 15 == 0: fizzbuzz_list.append('FizzBuzz') elif number % 5 == 0: fizzbuzz_list.append('Buzz') elif number % 3 == 0: fizzbuzz_list.append('Fizz') else: fizzbuzz_list.append(number) number += 1 return fizzbuzz_list print(fizzbuzz(16))

quite straight forward with for loop and if statements.

def fizzbuzz(limit): # output is listy. listy = [] # for all numbers from 1 to limit, for i in range(1, limit+1): # check if it is a multiple of 15 if i % 15 == 0: listy.append('FizzBuzz') # check if it is a multiple of 3 elif i % 3 == 0: listy.append('Fizz') # check if it is a multiple of 5 elif i % 5 == 0: listy.append('Buzz') # if not return the original number. else: listy.append(i) return listy print(fizzbuzz(16))

Haven’t seen anyone use the method that I used, though many did come close. I don’t know if it’s the best solution but I wanted to share it

def fizzbuzz(limit): # Write your code here x = [] for i in range(1, limit + 1): y = "" if i % 3 == 0: y = "Fizz" if i % 5 == 0: y = "Buzz" if i % 3 == 0 and i % 5 == 0: y = "FizzBuzz" x.append(y or i) return x print(fizzbuzz(16))

Given you have y declared as the empty string, the last conditional is redundant.

y = ""
if i % 3 == 0:
  y += 'Fizz'
if i % 5 == 0:
  y += 'Buzz'
x.append(y or i)
1 Like