Python Challenge - FizzBuzz, FizzBuzz

def multipleOfThree(number):
if number % 3 == 0:
return True
else:
return False

def multipleOfFive(number):
if number % 5 == 0:
return True
else:
return False

def fizzbuzz(limit):
fizzbuzz_list =
for i in range(1, limit + 1):
if multipleOfThree(i) and multipleOfFive(i):
fizzbuzz_list.append(“FizzBuzz”)
elif multipleOfThree(i):
fizzbuzz_list.append(“Fizz”)
elif multipleOfFive(i):
fizzbuzz_list.append(“Buzz”)
else:
fizzbuzz_list.append(i)
return fizzbuzz_list

print(fizzbuzz(16))

My first attempt at this challenge

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 % 3 == 0: list.append("Fizz") elif i % 5 == 0: list.append("Buzz") else: list.append(i) return list print(fizzbuzz(16))

easy solution to implement:
def fizzbuzz(limit):
result =
for num in range(1, limit + 1):
if num % 3 == 0 and num % 5 == 0:
result += [“FizzBuzz”]
continue
elif num % 3 == 0:
result += [“Fizz”]
continue
elif num % 5 == 0:
result += [“Buzz”]
continue
result += [num]
return result

print(fizzbuzz(16))

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

Line 5 could leverage the LCM rather than using logic:

if n % 15 == 0:

Your right!, it can be shorter

1 Like

Easy as pie

def fizzbuzz(limit): # Write your code here result = [] for i in range(1,limit + 1): three = i % 3 == 0 five = i % 5 == 0 if three & five: result.append("FizzBuzz") elif five: result.append("Buzz") elif three: result.append("Fizz") else: result.append(i) return result print(fizzbuzz(16))

Here is my solution. In the function, define a list called results. Iterate from 1 to the limit. If the iterator is a multiple of 3 and 5, add FizzBuzz to the list. If the iterator is a multiple of 3, but not a multiple of 5, add Fizz to the list. If the iterator is a multiple of 5, but not a multiple of 3, add Buzz to the list. Otherwise, add the number to the list.

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

print(fizzbuzz(16))

Heres my code. A pretty easy one in my opinion.

def fizzbuzz(limit):
  done = []
  for i in range(1, limit + 1):
    done.append(i)
    if (i % 5 == 0) & (i % 3 == 0):
      done.remove(i)
      done.append("FizzBuzz")
    elif i % 3 == 0:
      done.remove(i)
      done.append("Fizz")
    elif i % 5 == 0:
      done.remove(i)
      done.append("Buzz")
  return(done)


print(fizzbuzz(16))

Easy, yes, and one of which many iterations have been devised. Worth a think, though, in qualitative terms. This article makes a good point:

FizzBuzz. Do you actually know how to code? | by Wendy Raven McNair | Geek Culture | Medium

Consider this piece of persuasion:

"""
Created on Mon Sep  2 20:28:54 2024

@author: Mr as mtf
"""

def fbz(x, n = 1):
    return [
        "FizzBuzz" if not(i % 15) else
        "Fizz" if not(i % 3) else 
        "Buzz" if not(i % 5) else 
        str(i) 
        for i in range(n, x + 1)
    ]

print (fbz(30, 11))
['11', 'Fizz', '13', '14', 'FizzBuzz', '16', '17', 'Fizz', '19', 'Buzz', 'Fizz', '22', '23', 'Fizz', 'Buzz', '26', 'Fizz', '28', '29', 'FizzBuzz']

Like the one above, it is another take, no more, no less. We can play with this problem ad infinitum, it is that much fun, which to my mind means something of note.


A Ruby exploration:

FizzBuzz In Too Much Detail — Tom Dalling


print (fbz(30))
print (fbz(30, 31))
print (fbz(30, -11))
['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz', '16', '17', 'Fizz', '19', 'Buzz', 'Fizz', '22', '23', 'Fizz', 'Buzz', '26', 'Fizz', '28', '29', 'FizzBuzz']
[]
['-11', 'Buzz', 'Fizz', '-8', '-7', 'Fizz', 'Buzz', '-4', 'Fizz', '-2', '-1', 'FizzBuzz', '1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz', '16', '17', 'Fizz', '19', 'Buzz', 'Fizz', '22', '23', 'Fizz', 'Buzz', '26', 'Fizz', '28', '29', 'FizzBuzz']

Let’s see if we can get that into a working CodeByte:

def fbz(x, n = 1): return [ "FizzBuzz" if not(i % 15) else "Fizz" if not(i % 3) else "Buzz" if not(i % 5) else str(i) for i in range(n, x + 1) ] print (fbz(30, 11)) print (fbz(30)) print (fbz(30, 31)) print (fbz(30, -11))
1 Like

Hey mates, I would like to share this code I wrote to solve my first challenge, the “FizzBuzz”, in case it might help anyone who is a beginner in Python like me.

Feel free to comment on whether I could do better or how you proceed to solve the problem differently :wink:
Happy coding !!!

def fizzbuzz(limit): # Write your code here # first let's build the list list_number=[] for i in range(limit): list_number.append(i+1) # print(list_number) # then, let's replace items`Preformatted text` for i in range(len(list_number)): if list_number[i]%3==0 and not list_number[i]%5==0: list_number[i]='Fizz' elif list_number[i]%5==0 and not list_number[i]%3==0: list_number[i]='Buzz' elif list_number[i]%3==0 and list_number[i]%5==0: list_number[i]='FizzBuzz' # print(list_number) return list_number print(fizzbuzz(16)) print(fizzbuzz(3))