Another un-posted thing from a few years ago!  I guess I figured we had already discussed on twitter so “what’s the point”.  Still, nice to summarise with a few more words here…

Chris Cole (@drchriscole) posted a link back in 2019 to

https://www.r-bloggers.com/fizzbuzz-in-r-and-python/

on twitter (you can see the thread here), which poses a simple coding problem that is claimed to be used often to test a candidate’s coding knowledge:

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 author of the article shows some simple and clear ways to solve this problem in R and Python that would certainly convince a possible employer that you knew how to do basic coding.

`for (i in 1:100){    if(i%%3 == 0 & i%%5 == 0) {    print('FizzBuzz')  }  else if(i%%3 == 0) {    print('Fizz')  }  else if (i%%5 == 0){    print('Buzz')  }  else {    print(i)  }  }`

Chris though correctly pointed out you can solve the problem with only two “if” statements and wrote some pseudocode to show how:

`I only have my phone, but pseudo-code would be something like this.for i in 1:100outstr = ""num = iif i divisible by three outstr = "fizz" num = ""if i divisible by five  outstr = outstr + "buzz"  num = ""print num + outstr`

The big strength of R though is that it is a vector language so I (@gjbarton) came up with the following:

`My version in R but with three “if”s. There is prob. a really elegant way to do this in R...a = 1:100b = a %% 3c = a %% 5d = b + ct = replace(a,b==0,"Fizz")t = replace(t,c==0,"Buzz")t = replace(t,d==0,"FizzBuzz")tI'm a very rusty R programmer, first code in at least 3 years probably :-(`

Here I create three vectors, b,c,d that have zeros at positions where the number is divisible by 3, 5, or both 3 and 5.  I then substitute the appropriate text for zero in the result array t using the built-in “replace” function.

David Martin pointed out that I actually used zero if statements… so I guess this is a “win” ?

You might ask why solve the problem like this?  It appears to hide the basic logic that is very clear in the first solution with an “if” block.   For a very small task like this, the “if” block is probably clearest, but as soon as you start to extend the logic, if blocks can get very messy and hard to read.  For example, it would be easy to extend the vector method to include more options such as “also divisible by 7” or to explore all combinations while still maintaining fairly concise code.

There is also an efficiency consideration as the size of the vector grows.  Generally, computers are great at running down a big vector and doing the same simple operation on it.   Once you introduce complex nested “if” statements on each element of the vector then the code has multiple possible branches and so takes more steps to execute at the CPU level.  This can mean it runs a lot slower or that it might run into memory issues.   If the code is compiled though, the compiler might be able to spot things like this (The people who write compilers are true artists!) and so there will be no performance advantage.