I made a new friend this week (you should totally follow @ marcopennekamp on Twitter btw) who’s a fan of programming. Well, by that I mean he is a programmer. Anyway, he introduced me to a problem known as “The fizzbuzz problem“, which is a test of coding ability for Computer Science job applicants. It’s interesting, because the problem isn’t complicated at all, yet “99% of applicants can’t do it”. The problem is thus:
for the integers 1 to 100, print the number and then the word “fizz” if it’s a multiple of 3, the word “buzz” if it’s a multiple of 5, and “fizzbuzz” if it’s a multiple of both.
This is not a difficult thing to achieve, and any programmer worth a salary should just be able to write an iterated loop with three logic statements in. They *should* be able to. Anyway, since I’m more interested in technical computing (i.e. calculations) than computer science, I see this kind of thing all the time. I didn’t really see what all the fuss was about. I got interested in it, though, when I saw the solutions on the web. All of them, all of them were of an identical structure;
for (i=1 to i=100)
if (i is a multiple of 3 and 5) print (i and fizzbuzz)
else if (i is a multiple of 3) print (i and fizz)
else if (i is a multiple of 5) print (i and buzz)
else print (i)
Which struck me as odd. I’m a fan of Matlab, and I have to say that although that’s easy to write it’s not really interesting as such. So I made up my own problem; can I write the fizzbuzz program without writing any kind of loop?
Well, you bet I wouldn’t be writing this post if I hadn’t found a way!
Matlab has a function which is dear to my heart which is called “logical indexing”. This means that when you call an array, you can use a logical check (eg. i==1) to point to cells which satisfy the logical condition. Example: if you want to find elements of A which are integer multiples of two, you call A(mod(A,2)==0), where mod(A,2)==0 is the logical check. The problem was then finding a good way to get Matlab to write things out without using a loop. This turned out to be a lot more difficult, as Matlab is picky in how it handles strings (which are essential in this problem).
I eventually made use of a type of data structure called a “cell array” which has no restrictions on what you put in it. I won’t go into how they work, as frankly it’s boring. Let’s just jump straight to the code;
%script to demonstrate FizzBuzz
%create cell array, first column should be integers 1-100. also %spacer column
i = transpose(linspace(1,100));
c(1:100,1) = cellstr( int2str( i ) );
c(1:100,2:3) = cellstr(”);
spacer(1:100,1) = ‘ ‘;
%logic only requires two logical index functions
c(mod(i,3)==0,2) = cellstr(‘fizz’);
c(mod(i,5)==0,3) = cellstr(‘buzz’);
%string array for printing
[ char(c(:,1)) spacer char( strcat(c(:,2),c(:,3)) ) ]
So as you can see, using the character and string arrays meant that instead of writing a set of logic checks (total of four in the example I gave at the start), you can do it in two in Matlab! And all without actually using a loop. The result looks like this;
Which satisfies the problem nicely. I find little challenges like this fun, especially when you can write the program in as many lines as the typical iterated version (ignoring comments, it was seven, although early versions were as long as 9).