FizzBuzz question

In a recent blog post, a simple problem (called “FizzBuzz“) is solved. This problem is asked by some employers in data scientist job interviews. The question seeks to ascertain the applicant’s familiarity with basic programming concepts. 
https://www.r-bloggers.com/fizzbuzz-in-r-and-python/

The FizzBuzz Question
I came across the FizzBuzz question in this excellent blog post on conducting data scientist interviews and have seen it referenced elsewhere on the web. The intent of the question is to probe the job applicant’s knowledge of basic programming concepts.

The prompt of the question is as follows:

In pseudo-code or whatever language you would like: 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”.

The Solution in R
We will first solve the problem in R. We will make use of control structures: statements “which result in a choice being made as to which of two or more paths to follow.” This is necessary for us to A) iterate over a number sequence and B) print the correct output for each number in the sequence. We will solve this problem in two slightly different ways.

The author of the blog provided two solutions but I liked none of them. Here is my solution that is 5 times more rapid than the propositions but the author of the blog :

number_sequence_f <- as.character(1:100)
number_sequence_f[seq(from=3, to=100, by=3)] <- "Fizz"
number_sequence_f[seq(from=5, to=100, by=5)] <- "Buzz"
number_sequence_f[seq(from=15, to=100, by=15)] <- "FizzBuzz"
print(number_sequence_f)

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04