Claudio Cherubino's blog Life of a Googler

23Jan/093

Facebook FizzBuzz

Have you ever tried looking for "FizzBuzz" on a search engine?

If you do, you'll surely land on this page on Coding Horror, the blog written by Jeff Atwood.

To make a long story short, Jeff states that the vast majority of the developers is unable to write a tiny program that should take no more than 10 minutes to code.

This is the famous FizzBuzz problem:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

You should be outraged now, if you aren't I hope you don't make a living as a developer! :)

I found out that Facebook also considers FizzBuzz as a good starting test for job applicants. There is a programming puzzles page, where you can find a set of good problems, ranging from blindingly easy to very hard.

The first one is called HoppityHop! and it is just a variation of the good old FizzBuzz:

The program should iterate over all integers (inclusive) from 1 to the number expressed by the input file. For example, if the file contained the number 10, the submission should iterate over 1 through 10. At each integer value in this range, the program may possibly (based upon the following rules) output a single string terminating with a newline.

* For integers that are evenly divisible by three, output the exact string Hoppity, followed by a newline.
* For integers that are evenly divisible by five, output the exact string Hophop, followed by a newline.
* For integers that are evenly divisble by both three and five, do not do any of the above, but instead output the exact string Hop, followed by a newline.

Being a developer myself I couldn't resist writing a solution for this problem, obviously in F#:

#light

let HoppityHop n =
  let printHop x =
    match x with
    | x when (x % 15 = 0)-> printfn "Hop"
    | x when (x % 3 = 0) -> printfn "Hoppity"
    | x when (x % 5 = 0) -> printfn "Hophop"
    | _ -> ()
  [1 .. n] |> Seq.iter printHop

I know many of you will feel the urge to suggest a better solution, you're welcome, the comment area is yours to use!

Comments (3) Trackbacks (0)
  1. Hi Claudio.
    Clarissmo!
    Grazie.
    Ciao bene.

  2. Unnecessary use of active patterns :-)

    let FB n =
        let (|Fizz|Buzz|FizzBuzz|Numeric|) n =
            match n with
            | n when(n % 15 = 0) -> FizzBuzz("FizzBuzz")
            | n when(n % 5 = 0) -> Buzz("Buzz")
            | n when(n % 3 = 0) -> Fizz("Fizz")
            | n -> Numeric(n.ToString())
    
        let FizzBuzzImpl n =
            match n with
            | FizzBuzz(str)| Buzz(str) | Fizz(str) | Numeric(str) -> printf "%s" str
    
        [1..n] |> Seq.iter FizzBuzzImpl
    
  3. @Claudio — One thing to pay attention to is your unnecessary creation of a linked list [1..n] — very large values of n can cause a lot of memory to be consumed. In this case a sequence expression might be the most efficient route :)

    @Jason — nice!


Leave a comment

(required)

No trackbacks yet.