FizzBuzz is very simple problem, and as such its concise solutions are often not extendible and use few abstractions - making them less interesting when comparing solutions across different languages.
FizzBuzzBazz is a variant of FizzBuzz which adds on additional rule - for multiples of 7, print Bazz
. As such instead of the number 35, one would print BuzzBazz
, as it is both a multiple of 5, and a multiple of 7. This adds just enough complexity as to make its different solutions interesting for comparison.
I challenge you to implement FizzBuzzBazz in the language of your choice, and find the shortest correct program you can come up with. Post your solution in the comments below, or tweet with the hashtag #fizzbuzzbazz - I will update this page to show the shortest program submitted.
Current winner: 62 characters.
The rules of FizzBuzzBazz stand as follows:
Fizz
.
Buzz
.
Bazz
.
FizzBazz
, at is both a multiple of 3, and a multiple of 7.
My solution in LiveScript is 64 characters (can you beat that in the language of your choice?):
[1 to 100]map ->[k+\zz for k,v of{Fi:3,Bu:5,Ba:7}|it%v<1]*''||it
The solution is not in the recommended style, but in the interest of brevity I have removed optional characters.
LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
Now, for an explanation:
We have to print a list 100 items in a accordance with a certain set of rules. Our starting point is thus a list of the numbers from 1 to 100. To do this we will use a range literal:
[1 to 100]
We want to create a new list of the same amount of items by applying the same rule to each member of the list. For this we can use the map
function, which is a property of all arrays in the newer versions of JavaScript (and thus LiveScript).
[1 to 100].map(...)
map
takes a function as its argument, which it applies to each of member of the inputted list. The function which map
takes must have a single argument, and return a value which will be in corresponding value in the outputted list. In LiveScript, functions are defined with an arrow, pointing from the arguments to the function body.
[1 to 100].map((x) -> ...)
But how will decide what to return for each input to our map function?
The rules stipulate that for multiples of 3, we print Fizz, for multiples of 5, we print Buzz, and for multiples of 7, we print Bazz. We can represent this relationship with an object literal:
{Fizz: 3, Buzz: 5, Bazz: 7}
We need to transform this set of rules into output. As it is possible to have a number which is a multiple of more than one of those numbers, it is possible for more than one of the rules to pass. Thus, we must filter the object and produce a list of the results. An empty list will be the case when none of the rules pass.
To do this we can use a list comprehension over the object. Using of
gives us both the key and value for each item in the object. We want to output the key (eg. Fizz
) by filtering using a certain set of rules upon the value (eg. 3). The format is [output for values of input when condition]
[key for key, value of {Fizz: 3, Buzz: 5, Bazz: 7} when ...]
The condition is fairly straight forward, a number is a multiple of another if there is no remainder when divided. (Remember x
is the value we are mapping over.)
[key for key, value of {Fizz: 3, Buzz: 5, Bazz: 7} when x % value == 0]
This results in a list, but we do not want a list, we need a string. We thus join the list.
[key for key, value of {Fizz: 3, Buzz: 5, Bazz: 7} when x % value == 0].join('')
Putting together what we have so far - note that LiveScript implicitly returns the last expression in the function body (unless otherwise specified), so no return
statement is required.
[1 to 100].map((x) -> [key for key, value of {Fizz: 3, Buzz: 5, Bazz: 7} when x % value == 0].join('') )
The above doesn't give us what we want though - instead of printing an empty string when none of the rule pass, we want to print the number being inputted. In JavaScript, an empty string is "falsy". Thus, we can use the or
operator - it evaluates to the left hand side when the left is truthy, and the right hand side when the left is falsy.
[1 to 100].map((x) -> [key for key, value of {Fizz: 3, Buzz: 5, Bazz: 7} when x % value == 0].join('') or x )
It works! The above evaluates to a list with the proper results. However, it is a bit long, 109 characters, so we can shorten it up a bit.
First, parentheses are not required when calling a function in most cases.
[1 to 100].map (x) -> [key for key, value of {Fizz: 3, Buzz: 5, Bazz: 7} when x % value == 0].join('') or x
We can also shorten up the variable names we are using:
[1 to 100].map (x) -> [k for k, v of {Fizz: 3, Buzz: 5, Bazz: 7} when x % v == 0].join('') or x
When a function has only one argument, we can use it
to implicitly refer to that argument without explicitly defining it. Thus it
replaces x
in this case.
[1 to 100].map -> [k for k, v of {Fizz: 3, Buzz: 5, Bazz: 7} when it % v == 0].join('') or it
If the right hand side of the multiplication op *
is a string literal, then it acts to join the list on the left hand side. We can thus replace .join('')
.
[1 to 100].map -> [k for k, v of {Fizz: 3, Buzz: 5, Bazz: 7} when it % v == 0] * '' or it
|
is an alias for when
.
[1 to 100].map -> [k for k, v of {Fizz: 3, Buzz: 5, Bazz: 7} | it % v == 0] * '' or it
For our purposes we do not need worry about negative numbers, and we are dealing with only integers, so < 1
is equivalent to == 0
.
[1 to 100].map -> [k for k, v of {Fizz: 3, Buzz: 5, Bazz: 7} | it % v < 1] * '' or it
We're doing pretty good, but we can go further. Dots are optional in unambiguous situations, so we can shorten up the map
property access.
[1 to 100]map -> [k for k, v of {Fizz: 3, Buzz: 5, Bazz: 7} | it % v < 1] * '' or it
Factoring out the zz
found in all our rules, using a a word literal in the form of \word
, leaves us with:
[1 to 100]map -> [k + \zz for k, v of {Fi: 3, Bu: 5, Ba: 7} | it % v < 1] * '' or it
Finally, we can remove a bunch of whitespace. Because or
requires whitespace around it, we can swap it out for ||
which does not.
[1 to 100]map ->[k+\zz for k,v of{Fi:3,Bu:5,Ba:7}|it%v<1]*''||it
The final result of our code golf efforts: a total of 64 characters.
Note that our solution is easily extensible as our rules are stated as data rather than imperative code.
Thank you to Ian Barfield for creating an even shorter solution in LiveScript!
[++x>?[k+\zz for k,v of{Fi:3,Bu:5,Ba:7}|x%v<1]*'' for x to 99]
So far, the LiveScript solution of 62 characters is the shortest working FizzBuzzBazz program. Think you can beat it in the language of your choice? Enter your attempt in the comments below, or tweet with the hashtag #fizzbuzzbazz. If a shorter program is found, I'll update this section with the new results. For more information on LiveScript, check out its official site. If you are interested in functional programming, you may want to check out Functional Programming in JavaScript using LiveScript and prelude.ls.
For more on LiveScript and prelude.ls, follow @gkzahariev.
comments powered by Disqus