Cover Image

Fizz Buzz

738 words. Time to Read: About 7 minutes.

There is a pretty common interview/beginner coding puzzle called “Fizz Buzz.” It’s conceptually not that difficult, but there are a lot of ways to do it, and I’m sure lots of people have strong opinions on the absolute best way to complete it. I thought I’d share a few answers here. Here’s the problem (or, at least, one if its variants).

List out the numbers from 1 to 100. If the number is a multiple of 3, print “Fizz.” If the number is a multiple of 5, print “Buzz.” If the number is a multiple of 3 and 5, print “FizzBuzz.” Otherwise, just print the number.

The Naive (Bluntest) Solution

def fizzbuzz(limit):
    """Print the numbers from 1 to limit (exclusive).
    Multiples of 3 say "Fizz"
    Multiples of 5 say "Buzz"
    Multiples of 3 and 5 say "FizzBuzz"
    Everything else says the number"""
    for number in range(1, limit):
        # Do 15 first to avoid double-printing
        if number % 3 == 0 and number % 5 == 0:
            print("FizzBuzz")
        elif number % 3 == 0:
            print("Fizz")
        elif number % 5 == 0:
            print("Buzz")
        else:
            print(number)
if __name__ == "__main__":
    fizzbuzz(101)

This solution works, but there’s some efficiency things that we can do to clean up a few loose ends.

The Improved If-Then Solution

def fizzbuzz(limit):
    """Same as above, but mo' bettah"""
    for number in range(1, 100):
        result = ""
        if number % 3 == 0: result += "Fizz"
        if number % 5 == 0: result += "Buzz"
        # Doing it this way we skip having to do 15!
        print(result or number) # Short circuit or if result is not falsy

A little shorter, a little easier to read. Let’s keep going.

Less Ifs Solution

def fizzbuzz(limit):
    """Fizz Buzz again!"""
    outputs = {3: "Fizz",
                5: "Buzz",
                15: "FizzBuzz"}
    for number in range(1, limit):
        result = ""
        for key, value in outputs.items():
            if number % key == 0 and len(value) >= len(result):
                # The length checks are necessary because you can't
                # depend on the order of the dict results.
                # FizzBuzz might get overwritten as Fizz OR Buzz otherwise!
                result = value
        print(result or number)

In my opinion, not better or clearer, but it has an interesting use of a dictionary which is kind of neat. From here, you can kind of get more and more into abstract solutions, but it kind of gets Rube Goldberg-ish, seeming like a lot of work for a simplish task. One more with a little functional pythonic twist.

Listy Solution

def fizzbuzz(number):
    """Returns the fizzbuzz result for one number"""
    if number % 15 == 0: return "FizzBuzz"
    if number % 3 == 0: return "Fizz"
    if number % 5 == 0: return "Buzz"
    return str(number)

results = [fizzbuzz(num) for num in range(1, 101)]
print(results)

I think this is my favorite. If you’ve got a fun solution that’s different than these, share it! [email protected]

Author: Ryan Palo | Tags: python puzzle | Buy me a coffee Buy me a coffee

Like my stuff? Have questions or feedback for me? Want to mentor me or get my help with something? Get in touch! To stay updated, subscribe via RSS