Reading about interview questions is always a geektrap, calling for writing the program in
the fastest (if it's a question of speed) or most perverse (if it's not) way
possible. I feel I've risen to the challenge on this occasion, with several
stomach-turning programs.
Files currently attached to this page:
fb.Makefile
fb.Makefile: This one is simple: It breaks the numbers down into 4
subsets: Regular, fuzz, buzz, and fuzzbuzz. For each one, it defines a rule
that prints the proper thing. The default rule depends on all the numbers from
1 to 100, so it invokes all the number rules in order.
fb.m4
fb.m4:
This one is straightforward, but in an ugly language, and must use recursion
instead of iteration.
fb.bash
fb.bash: This one is a bit silly, using bash, seq, dc, and awk. 100 lines are printed
by the 'for' loop, which consist of the number first in binary and then in
decimal. The awk script uses a regular expression to match multiples-of-three
written in binary, and another regular expression (a much simpler one)
to match multiples-of-five written in decimal.
fb.cc
fb.cc: I'm proudest of this one. It uses C++ templates to
produce a heavily nested class X<100>. Each X<int> class but X<0> contains a
Y<> class. Y<> is specialized to print the correct thing for each of the 4
cases. Each X<int> but X<0> also contains an X<i-1>, and so on recursively.
By the C++ rules concerning order of construction, the output is printed in the
desired order. To make things even more efficient, when a number is to be
printed, it's converted to ASCII at compile time.
fb.py
fb.py:
I couldn't resist finding a perverse way to write this in Python, since the
clear ways can be so very clear. Unfortunately, compose() is way too clear,
and anyone familiar with how to use the "ternary-like" operator in Python will
catch on to the fuzz and buzz functions right away.
fb2.py
fb2.py:
Another Python program. Using 'lambda' makes everything clearer, doesn't it!
fb.c
fb.c:
I love using the C preprocessor, but I've never done this kind of
self-inclusion before. Won't run if sizeof(char*) != sizeof(int).