“FizzBuzz” Test

So I read about simple test given in interviews to applicants for programming positions. Write a program that prints the integers i from 1 to 100 with the following exceptions:

  1. If i is a multiple of 3 (i\pmod{3} = 0) print “Fizz”;
  2. If i is a multiple of 5 (i\pmod{5}=0), print “Buzz”;
  3. If i is a multiple of both 3 and 5 (i\pmod{15}=0), print “FizzBuzz”.

Apparently, a large percentage of applicants struggle with this task. I guess I did as well: I couldn’t remember the arithmetic operator for modular arithmetic, and I omitted a logical condition in my first draft. I am not a programmer, and I do not know many programming languages; but I am becoming pretty familiar with R, so I wrote an R script that does as desired above. Here’s my code:

for (i in 1:100) {
  if (((i %% 3) == 0) & ((i %% 5) > 0)) {
    print("Fizz")
  } else if (((i %% 5) == 0) & ((i %% 3) > 0)) {
    print("Buzz")
  } else if ((i %% 15) == 0) {
    print("FizzBuzz")
  } else {
    print(i)
  }
}
Advertisements
This entry was posted in Progamming, R and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s