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

always fun to practice this one

def fizzbuzz(n): fb_lst = [] for i in range(1, n + 1): if i % 3 == 0 and i % 5 == 0: fb_lst.append("FizzBuzz") elif i % 3 == 0: fb_lst.append("Fizz") elif i % 5 == 0: fb_lst.append("Buzz") else: fb_lst.append(i) return fb_lst print(fizzbuzz(16))
def fizzbuzz(limit):
  # Write your code here
  numbers = [i + 1 for i in range(limit)]
  fizzbuzz = []
  for num in numbers:
    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(n): #fizzbuzz fonksiyonu oluştur
    fizzbuzz_list = [] #
    for i in range(1, n+1): #1 den n e kadar döngü oluştur
        if i % 3 == 0 and i % 5 == 0: #3 ve 5 e bölünen sayılar için
            fizzbuzz_list.append("FizzBuzz") #fizzbuzz_list'e FizzBuzz ekler
        elif i % 3 == 0: #3 e bölünen sayılar için
            fizzbuzz_list.append("Fizz")
        elif i % 5 == 0: #5 e bölünen sayılar için
            fizzbuzz_list.append("Buzz")
        else: #3 ve 5 e bölünmeyen sayılar için
            fizzbuzz_list.append(i)
    return fizzbuzz_list
#test
print(fizzbuzz(35))

We’ll be assuming you have List comprehensions in your toolbox by this point in your studies:

>>> [f"{'Fizz' if not x % 3 else ''}{'Buzz' if not x % 5 else ''}" or x for x in range(100)]
['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', 31, 32, 'Fizz', 34, 'Buzz', 'Fizz', 37, 38, 'Fizz', 'Buzz', 41, 'Fizz', 43, 44, 'FizzBuzz', 46, 47, 'Fizz', 49, 'Buzz', 'Fizz', 52, 53, 'Fizz', 'Buzz', 56, 'Fizz', 58, 59, 'FizzBuzz', 61, 62, 'Fizz', 64, 'Buzz', 'Fizz', 67, 68, 'Fizz', 'Buzz', 71, 'Fizz', 73, 74, 'FizzBuzz', 76, 77, 'Fizz', 79, 'Buzz', 'Fizz', 82, 83, 'Fizz', 'Buzz', 86, 'Fizz', 88, 89, 'FizzBuzz', 91, 92, 'Fizz', 94, 'Buzz', 'Fizz', 97, 98, 'Fizz']
>>> 

It should be stipulated that if zero is divisible by 2 then it is divisible by anything that is not zero; hence, 0 is FizzBuzz.

With this we can poll a number’s FizzBuzz status:

>>> def fizzbuzz(n):
...     return [f"{'Fizz' if not x % 3 else ''}{'Buzz' if not x % 5 else ''}" or x for x in range(n + 1)][n]
... 
>>> fizzbuzz(90)
'FizzBuzz'
>>> 

BigO time complexity is O(n) and memory is surrendered upon return so transient. The next trick would be to make this a generator so very little memory is ever used.

It should be be clear at this point that indices matter.

Thanks for your precious answer. Appreciated :slightly_smiling_face:

1 Like