Different implementations of the FizzBuzz problem using R Language
The FizzBuzz is a well-known problem in R Language. ‘Fizz’ refers to numbers that are multiples of three, ‘Buzz’ refers to numbers that are multiples of five, and ‘FizzBuzz’ refers to numbers that are multiples of both three and five.
Method 1: implementations of the FizzBuzz using Classical Approach
This is a classical approach to solving the FizzBuzz problem, We start by running the for loop for every number that exists in the sequence range. We have created a sequence using the ‘:’ operator. The ‘:’ operator takes the step size as 1. Now inside the for loop we have if, else if, and else conditional statements. We will then check the divisibility using the modulus operator (%%) of the number.
If the number is divisible by 15 we output ‘FizzBuzz’, else if we check that if a number is divisible by 3, if the number is divisible by 3 then we output ‘Fizz’, further we check if the divisible by 5, if the number is divisible by 5, we then output ‘Buzz’. If none of these conditions are satisfied the else conditional statement gets executed, where the number is directly outputted as it is.
R
for (number in 1:100) { if (number %% 15 == 0){ cat ( 'FizzBuzz\n' ) } else if (number %% 3 == 0){ cat ( 'Fizz\n' ) } else if (number %% 5 == 0){ cat ( 'Buzz\n' ) } else { cat (number) cat ( '\n' ) } } |
Output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 Fizz Buzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 Fizz Buzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 Fizz Buzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 Fizz Buzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 Fizz Buzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
Method 2: Efficient solution than the Classical using loop
In this solution, we try to reduce the number of conditional statements. Here, we are using paste0 because we want to concatenate without a separator. Let us assume the number is 9. We check the following condition ‘9%%3==0’. The output of the following condition will be True. So the statements inside the if condition will be executed. We are concatenating all elements that are o which is an empty string, and a new string “Fizz”. We then store all elements back in o. We finally display the result for each number.
R
for (number in 1:100) { o <- '' if (number%%3==0){ o<- paste0 (o, "Fizz" ) } if (number%%5==0){ o<- paste0 (o, "Buzz" ) } if (o== '' ){ o<-number } cat (o) cat ( '\n' ) } |
Output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 Fizz Buzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 Fizz Buzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 Fizz Buzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 Fizz Buzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 Fizz Buzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
Method 3: Implementations of the FizzBuzz using the function
In this solution, we try to eliminate them for loop and instead utilized a function sapply(). The sapply() function takes a vector as the input and returns the result of the function on the sequence that we have passed as an input. Then we have the code of a function that had a quite similar functioning as explained in solution 2.
R
fizz_buzz <- function (number){ o <- '' if (number%%3==0){ o<- paste0 (o, "Fizz" ) } if (number%%5==0){ o<- paste0 (o, "Buzz" ) } if (o== '' ){ o<-number } cat (o) cat ( '\n' ) } fb<- sapply (1:100, fizz_buzz)[0] |
Output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 Fizz Buzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 Fizz Buzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 Fizz Buzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 Fizz Buzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 Fizz Buzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
Method 4: Implementations of the FizzBuzz using package ‘fizzbuzzR’
Here we are utilizing a function namely ‘fizzbuzz’ in the ‘fizzbuzzR’ package. It takes the following arguments:
- start: This is the starting value of the loop
- end: The loop will end at this value
- mod1: If we specify mod1 as 3, we are then saying that we will replace all the divisors of 3 with ‘Fizz’
- mod2: If we specify mod2 as 5, we are then saying that we will replace all the divisors of 5 with ‘Buzz’
R
install.packages ( "fizzbuzzR" ) library (fizzbuzzR) fizzbuzz (start = 1, end = 100, mod1 = 3, mod2 = 5) |
Output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 Fizz Buzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 Fizz Buzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 Fizz Buzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 Fizz Buzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 Fizz Buzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
Please Login to comment...