wael.blog

Where I talk about anything and everything Code!

20, Jul 2024

Solving the FizzBuzz problem

The first time I came across the FizzBuzz problem was through a Youtube video by Tom Scott. I first thought *"Well that's easy!"*; alas, I was the naive one. Whether you're a beginner or an experienced coder, this exercise is a great way to hone your programming skills and put you in the interviewer seat for a bit. We'll dive into different solutions, ramping up the complexity and adding a sprinkle of Swift-specific elegance.




What is the FizzBuzz problem?



Loop through *"n", if the i is divisible by 3: print "Fizz". If i is divisible by 5: print "Buzz". If i• is divisible by both 3 & 5: print *"FizzBuzz". If none of the above: print the value of i*

Simple, right, almost .. too simple ...

The basic FizzBuzz solution



Let's start with the simplest approach. We'll use a loop to iterate through the numbers from 1 to 100 and apply our FizzBuzz logic. Here's how it looks:

for i in 1...100 {
    if i % 3 == 0 && i % 5 == 0 {
        print("FizzBuzz")
    } else if i % 3 == 0 {
        print("Fizz")
    } else if i % 5 == 0 {
        print("Buzz")
    } else {
        print(i)
    }
}


This code is straightforward. It checks if a number is divisible by both 3 and 5 first, then handles divisibility by 3 or 5 separately. This works, but you may have noticed that it uses the modulus operation % four different times per iteration, can we do better?

Let’s explore FizzBuzz with Swift specific twist:

*Using a `switch` statement:*

for i in 1...100 {
    switch (n % 3 == 0, n % 5 == 0) {
    case (true, true):
        print("FizzBuzz")
    case (true, false):
        print("Fizz")
    case (false, true):
        print("Buzz")
    default:
        print(i)
    }
}


In this version the `switch` statement Simplifies the conditional logic, the code more readable, and we can see that the modulus operand is used only twice per iteration. Notice that the same can be achieved with simple two boolean flags and `if` statements. But can we do even better?




Using counters:

var three = 1
var five = 1

for i in 1...100 {
    var out = ""

    if three == 3 {
      out = "Fizz"
      three = 1
    } else {
      three += 1
    }

    if five == 5 {
      out += "Buzz"
      five = 1
    } else {
      five += 1
    }

    if out.isEmpty {
        out = "\(i)"
    }

    print(out)
}


Here, no modulus operations are used, everything is counted step by step, once we counted "3" steps; add "Fizz" to the output. Once we count "5" steps, concatenate "Buzz" to the output. If the output string is empty, just assign the string value of 'i' to the output.

I personally think this is one of the best solutions to come up with during an interview, but make no mistake, there are even crazier solutions out there. I remember once seeing a one line python solution somewhere on the web!

Always keep this exercise at the back of your mind to sharpen your coding skills. I really encourage you to explore other solutions with different languages and contexts, you'd be surprised how many are out there. So next time you encounter FizzBuzz, whether in an interview or just for fun, be ready with a variety of solutions. Keep experimenting, keep coding, and most importantly; Enjoy it!

Interested in what I do? get in touch

Contacts

wael.studio

×
About Services Skills Projects Blog Contact