Fizz buzz started out as an innocent game to help children learn how to divide numbers. Over time it’s evolved into a test to filter out prospective applicants for various software engineering jobs.
The Fizz-Buzz test was invented by Imran Ghory in 2007 to interview software developers and to find out if the candidate can actually write code. The test is designed to be quite simple. Many still fall victim to it ironically due to its overly simplistic nature. Another reason some may fail is that they became familiar with a certain language by memorizing certain patterns, rather than being aware of the logic used in their programs. With that being said, there’s no need to feel discouraged. Failure is good for programmers as long as they learn from it. Today I’ll be walking through a common Fizz-Buzz problem and hopefully what you take away is not what code I used, but the logic behind the problem. I will be using Ruby to solve this problem but any language can be used to do so and once again, its all about the logic!
The problem is as follows:
Let’s list what this problem is asking us to do:
- Create a loop that counts up to 100 and ends if the number is over 100.
- If the number is divisible by 3 it returns “Fizz”
- If the number is divisible by 5 it returns “Buzz”
- If the number is divisible by both 3 and 5 it returns “FizzBuzz”
Ok let’s get started! Take a look at the code below.
Great! Now we have a loop set up that will count from 1 to 100. The upto method is a convenience method that does the same thing as a for loop.
Here, I set an empty string equal to the variable str to collect all the elements and puts str will output the final result. Remember, our goal is to output the strings “Fizz”, “Buzz”, and “FizzBuzz” when a number meets the requirements from the problem. The program will keep adding on to the empty string as it goes through the logic.
Now we have our logic inserted to the loop. The variable str will receive either the “Fizz” or “Buzz” with the help of the += assignment operator. We wouldn’t use the shovel method << in this case since it alters the original string rather than creating a new one. The modulo operator (%) is used to determine whether or not the number in question is divisible by 3 or 5. A modulo operation finds the remainder or signed remainder after division of one number by another. For example,
21/7 = 3.0
22/7 = 3.1428571428621 % 7 = 0
22 % 7 = 1
It’s imperative that you understand how modulo operations work, without it you would lack the ability to solve similar problems in the future.
Now when you run the file and check the results you should get…
Congratulations! Now you’ll be more than ready when your interviewer asks you to solve their FizzBuzz problem.
Now that you’re comfortable with FizzBuzz let’s take a look at another problem that’s quite similar. We’ll be looking at the first problem from projecteuler.net.
Projecteuler.net states that it is designed to challenge one’s problem solving and mathematics skills. A mathematics background is not necessary to complete the problems however, there are over 700 problems that get more difficult as you progress. The interesting part is that it involves inductive chain learning, meaning that by solving one problem it will expose you to a new concept that allows you to solve a previously inaccessible problem. Now let’s take a look at the first problem.
Wow! Would you look at that, it looks pretty similar to the FizzBuzz problem we did before. It seems our inductive chain learning process already started before we even embarked on our project Euler Journey!
Similar to the last problem, it’s asking us to print out a list of numbers and pull out the ones that are divisible by 3 or 5. This time instead of 1 to 100 it is asking for all numbers below 1000.
First we have to set the variable total = 0 to allow us to store all elements that are divisible by 3 or 5. Later on we will be able to call on the variable to add up the elements and return the sum.
Now we code in our if statement with the modulo operations that will collect all the necessary numbers. Then by using the assignment operator += we add the iterated numbers onto the total where it is then finally outputted.
Now lets run the code and see what our answer is:
Here’s the answer on projecteuler.net
Well 1 problem down and over 700 more to go ¯\_(ツ)_/¯. Just kidding, to be quite honest completing all the problems would be an incredible feat but just completing around 50 Project Euler problems would be enough to build up your computational logic. Don’t forget to push up your solutions to GitHub as well!