Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,17 @@
"filtering",
"strings"
]
},
{
"slug": "fizz-buzz",
"uuid": "b35ca909-3389-4427-810f-775aa174001f",
"core": false,
"unlocked_by": "raindrops",
"difficulty": 1,
"topics": [
"control_flow_conditionals",
"text_formatting"
]
}
]
}
29 changes: 29 additions & 0 deletions exercises/fizz-buzz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Fizz Buzz

Print numbers from 1 to n with the following exceptions:

1. When a number is a multiple of 3, then print "Fizz"
2. When a number is a multiple of 5, then print "Buzz"
3. When a number is both a multiple of 3 and a multiple of 5, then print "Fizz Buzz"

The output should be a vector of strings, such as, "1" "2" "Fizz" "4" "Buzz" ...

## Installation
See [this guide](https://exercism.io/tracks/r/installation) for instructions on how to setup your local R environment.

## How to implement your solution
In each problem folder, there is a file named `<exercise_name>.R` containing a function that returns a `NULL` value. Place your implementation inside the body of the function.

## How to run tests
Inside of RStudio, simply execute the `test_<exercise_name>.R` script. This can be conveniently done with [testthat's `auto_test` function](https://www.rdocumentation.org/packages/testthat/topics/auto_test). Because Exercism code and tests are in the same folder, use this same path for both `code_path` and `test_path` parameters. On the command-line, you can also run `Rscript test_<exercise_name>.R`.

## Source

"Fizz-Buzz" originated as a children's game that helped children learn to divide. It is now regularly used in coding interviews.

1. [Actual Source](https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding)
2. [Rosetta Code](https://rosettacode.org/wiki/FizzBuzz)
3. [Code Golf](https://code-golf.io/fizz-buzz)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
16 changes: 16 additions & 0 deletions exercises/fizz-buzz/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fizz_buzz <- function(input) {
replace_number <- function(i) {
if (i %% 3 == 0 && i %% 5 == 0) {
return("Fizz Buzz")
} else if (i %% 3 == 0) {
return("Fizz")
} else if (i %% 5 == 0) {
return("Buzz")
} else {
return(i)
}
}

out <- sapply(seq(1, input), replace_number)
return(out)
}
2 changes: 2 additions & 0 deletions exercises/fizz-buzz/fizz-buzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fizz_buzz <- function(input) {
}
25 changes: 25 additions & 0 deletions exercises/fizz-buzz/test_fizz-buzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
source("./fizz-buzz.R")
library(testthat)

ans10 <- c("1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz")

ans100 <- c(
"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"
)


test_that("Vector of strings match", {
expect_identical(fizz_buzz(10), ans10)
expect_identical(fizz_buzz(100), ans100)
})

message("All tests passed for exercise: fizz-buzz")