Notes on codes, projects and everything

Revisiting Fizz Buzz in PHP

Back then when I was attending a job interview, I was asked to write a Fizz Buzz program to prove that my coding ability. There was only a pen and a piece of paper, so basically means there’s no way I can refer to the documentation for the API syntax. Fortunately I somehow managed to remember and not screw up.

I didn’t know anything about the program, so the interviewer had to explain what he expects from the program. If I recall correctly, the problem was defined as given a list of integers from 1 to 50, print a ‘fizz’ if the number is a multiple of 3, and ‘buzz’ if 5 divides the number. I think my solution to the problem back then was rather simple, like this


<?php
 
for($i = 1; $i <= 50; $i++) {
    echo "$i ";
 
    if($i % 3 == 0 AND $i % 5 == 0) {
        echo 'fizz buzz';
    } else if($i % 3 == 0) {
        echo 'fizz';
    } else if($i % 5 == 0) {
        echo 'buzz';
    }
}

The interviewer back then apparently somehow satisfied, but he then told me it is possible to make it a one liner. While I was impressed, but I didn't really bother to find out at the time, it is just a trivial programming puzzle anyway, no? However, from time to time, I do think about the possibility.

Years later, a couple of days earlier I came across a random status posted by an ex-colleague of mine (iirc). I think the post was about implementing Fizz Buzz in golang, but I didn't really bother to read the article. While continue browsing through the timeline, I started thinking if I am to code a solution to this problem these days, how would I do it?

I started picking up habit to do a lot of array_map() and array_reduce() instead of traditional loop, back when I was nearing the end of my first job. Then while I was working on my now nearly abandoned research project I picked up Clojure to see if it is a practical tool to do data manipulation. While I decided to stick with PHP for the data manipulation task, the idea of doing it the functional way somehow stuck. And co-incidentally, PHP finally implemented a somehow more useful anonymous function syntax (no more create_function() for goodness sake).

So while I didn't bother to read the actual article about fizz buzz in golang, I decided to re-write fizz buzz in PHP the (pseudo-)functional way. The resulting code is shown below

#!/usr/bin/env php
<?php
 
array_walk(
    range(1, 50),
    function($number, $key, Closure $processor) {
        printf(
            '%s %s%s',
            $number,
            trim(implode(
                ' ',
                array_map(
                    function(Closure $printer) use($number) {
                        return call_user_func($printer, $number);
                    },
                    array(
                        call_user_func($processor, 3, 'fizz'),
                        call_user_func($processor, 5, 'buzz'))))),
            PHP_EOL
        );
    },
    function($divisor, $output) {
        return function($number) use($divisor, $output) {
            return $number % $divisor == 0 ? $output : NULL;
        };
    });

While it has a lot more line count, and is considerably longer (and more inefficient), but I think I like this version better. The only problem beside it being inefficient, and longer, is that there's no way I can write this without the aid of documentation. I mean who would remember (OK, I won't) the syntax difference between array_walk(), array_map(), array_filter() and array_reduce() etc.

Guess I should just stick with the first one if I am asked again in another job interview in future.

Related Posts Plugin for WordPress, Blogger...

leave your comment

name is required

email is required

have a blog?

This blog uses scripts to assist and automate comment moderation, and the author of this blog post does not hold responsibility in the content of posted comments. Please note that activities such as flaming, ungrounded accusations as well as spamming will not be entertained.

Pings

Click to change color scheme