- #1
- 37,794
- 10,187
- TL;DR Summary
- Screeners for applicants of programmer jobs have used Fizz Buzz for decades. A typical test would be to ask the applicant to write a program in any language that prints the integers from 1 to 100, with these exceptions. Numbers that are divisible by 3 should be replaced by "Fizz", numbers that are divisible by 5 should be replaced by "Buzz," and numbers that are divisible by 15 should be replaced by "FizzBuzz."
Fizz Buzz has been discussed in many online blogs are articles, in a multitude of programming languages. It has been widely used as a screening device for those aspiring to obtain developer positions. The job candidate is asked to write some code to print the numbers 1 through 100, with "Fizz" replacing numbers that are multiples or 3, "Buzz" replacing numbers that are multiples of 5, and "FizzBuzz" replacing numbers that are multiples of 15.
This has been surprisingly difficult for many applicants of programming positions.
Without further ado, here's my code:
Some points about the code, which would be invoked from the Erlang interpreter.
This has been surprisingly difficult for many applicants of programming positions.
Without further ado, here's my code:
Code:
-module(fizz).
-export([fb/1]).
fb(Max) -> fb(1, Max).
fb(Num, Max) ->
if Num == Max + 1 -> exit("Normal exit");
Num rem 15 == 0 -> io:format("FizzBuzz~n");
Num rem 3 == 0 -> io:format("Fizz~n");
Num rem 5 == 0 -> io:format("Buzz~n");
true -> io:format("~w~n", [Num])
end,
fb(Num + 1, Max).
Some points about the code, which would be invoked from the Erlang interpreter.
- The first two lines 1) identify this as the fizz module, and 2) export a function named fb (short for fizzbuzz) that takes one parameter.
- The fb() function is overloaded, with one version that takes a single parameter and another that takes two parameters. Only the single-parameter variant is exported.
- When fb(Max) is called from the interpreter, it immediately calls the two-parameter version via fb(1, Max).
- Unlike most other programming languages, Erlang doesn't have any syntax for loop constructs. Instead, it usually relies on recursion. The two-parameter version first checks whether the current value of Num is equal to Max + 1. If so, the program exits.
- Erlang if blocks consist of a number of patterns to be matched. In the program above, the patterns to be matched check whether Num is still in range (i.e., from 1 through Max), whether Num is divisible by 15, whether Num is divisible by 3, whether Num is divisible by 5, and finally a catchall if none of the previous patterns has been matched.
- All but the first pattern cause output of one of the strings "Fizz", "Buzz", or "FizzBuzz" to be displayed or the number itself. All are followed by a newline character.
- After the if block is a recursive call to fb(Num + 1, Max). This happens to be tail-call-recursive, which means that the recursive call is at the end of the code. The Erlang interpreter is able to transform tail-call-recursive functions into loops, ameliorating the problem of stack overflow that recursive functions are prone to in other languages.