Skip to content

Instantly share code, notes, and snippets.

@wcDogg
Last active January 21, 2023 06:59
Show Gist options
  • Save wcDogg/5d62e743b42d33141a63d131fb416623 to your computer and use it in GitHub Desktop.
Save wcDogg/5d62e743b42d33141a63d131fb416623 to your computer and use it in GitHub Desktop.
FizzBuzz in Python

FizzBuzz in Python

Like 3M other people inspired by Tom Scott's FizzBuzz: One Simple Interview Question video, here is my 20 minute stab at FizzBuzz. I imagine a programmer would nail this in under 10 :P

I did it this way because I was curious about the different patterns variable inputs would create. The results remind me of The Rythm of Primes :)

class FizzBuzz:
  '''Classic children's game in Python.
  '''
  def __init__(self, fizz:int=3, buzz:int=5, incr:int=1, stop:int=100) -> None:
    self.start: int = 1
    self.stop: int = stop + 1
    self.incr: int = incr
    self.fizz: int = fizz
    self.buzz: int = buzz
    self.fizzbuzz: int = fizz * buzz
    self.play()

  def play(self) -> None:
    ''''''
    print ("--------------------------------------------------")
    print (f"incr = {self.incr} | fizz = {self.fizz} | buzz = {self.buzz} | fizzbuzz = {self.fizzbuzz}")
    print ("--------------------------------------------------")

    turns: list = []

    for x in range(self.start, self.stop):
      if x % self.incr == 0:
        turns.append(x)

    for x in turns:
      if x % self.fizzbuzz == 0:
        print(f"{x}: FizzBuzz")
      elif x % self.fizz == 0:
        print(f"{x}: Fizz")
      elif x % self.buzz == 0:
        print (f"{x}: Buzz")
      else:
        print (f"{x}: {x}")      


if __name__ == "__main__":

  # Default - Odd, Odd, Odd
  game = FizzBuzz()

  # Odd, Odd, Even
  game = FizzBuzz(3, 7, 4)

  # Odd, Even, Even
  game = FizzBuzz(3, 4, 2)

  # Even, Even, Even
  game = FizzBuzz(4, 6, 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment