Skip to content

Commit f262f06

Browse files
committed
Add Fizz Buzz exercise
* Add files, questions, example answer and tests * Fix linting errors by lintr-bot * Add config (using UUID package) * Style all files using styler::style_directory() * Change con-cat()-enated string to c()-ombined vector of strings * Add different sources * Refactor function name
1 parent 53354bb commit f262f06

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@
381381
"filtering",
382382
"strings"
383383
]
384+
},
385+
{
386+
"slug": "fizz-buzz",
387+
"uuid": "b35ca909-3389-4427-810f-775aa174001f",
388+
"core": false,
389+
"unlocked_by": "raindrops",
390+
"difficulty": 1,
391+
"topics": [
392+
"control_flow_conditionals",
393+
"text_formatting"
394+
]
384395
}
385396
]
386397
}

exercises/fizz-buzz/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Fizz Buzz
2+
3+
Print numbers from 1 to n with the following exceptions:
4+
5+
1. When a number is a multiple of 3, then print "Fizz"
6+
2. When a number is a multiple of 5, then print "Buzz"
7+
3. When a number is both a multiple of 3 and a multiple of 5, then print "Fizz Buzz"
8+
9+
The output should be a vector of strings, such as, "1" "2" "Fizz" "4" "Buzz" ...
10+
11+
## Installation
12+
See [this guide](https://exercism.io/tracks/r/installation) for instructions on how to setup your local R environment.
13+
14+
## How to implement your solution
15+
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.
16+
17+
## How to run tests
18+
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`.
19+
20+
## Source
21+
22+
"Fizz-Buzz" originated as a children's game that helped children learn to divide. It is now regularly used in coding interviews.
23+
24+
1. [Actual Source](https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding)
25+
2. [Rosetta Code](https://rosettacode.org/wiki/FizzBuzz)
26+
3. [Code Golf](https://code-golf.io/fizz-buzz)
27+
28+
## Submitting Incomplete Solutions
29+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/fizz-buzz/example.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fizz_buzz <- function(input) {
2+
replace_number <- function(i) {
3+
if (i %% 3 == 0 && i %% 5 == 0) {
4+
return("Fizz Buzz")
5+
} else if (i %% 3 == 0) {
6+
return("Fizz")
7+
} else if (i %% 5 == 0) {
8+
return("Buzz")
9+
} else {
10+
return(i)
11+
}
12+
}
13+
14+
out <- sapply(seq(1, input), replace_number)
15+
return(out)
16+
}

exercises/fizz-buzz/fizz-buzz.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fizz_buzz <- function(input) {
2+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
source("./fizz-buzz.R")
2+
library(testthat)
3+
4+
ans10 <- c("1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz")
5+
6+
ans100 <- c(
7+
"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz",
8+
"13", "14", "Fizz Buzz", "16", "17", "Fizz", "19", "Buzz", "Fizz", "22", "23",
9+
"Fizz", "Buzz", "26", "Fizz", "28", "29", "Fizz Buzz", "31", "32", "Fizz",
10+
"34", "Buzz", "Fizz", "37", "38", "Fizz", "Buzz", "41", "Fizz", "43", "44",
11+
"Fizz Buzz", "46", "47", "Fizz", "49", "Buzz", "Fizz", "52", "53", "Fizz",
12+
"Buzz", "56", "Fizz", "58", "59", "Fizz Buzz", "61", "62", "Fizz", "64",
13+
"Buzz", "Fizz", "67", "68", "Fizz", "Buzz", "71", "Fizz", "73", "74",
14+
"Fizz Buzz", "76", "77", "Fizz", "79", "Buzz", "Fizz", "82", "83", "Fizz",
15+
"Buzz", "86", "Fizz", "88", "89", "Fizz Buzz", "91", "92", "Fizz", "94",
16+
"Buzz", "Fizz", "97", "98", "Fizz", "Buzz"
17+
)
18+
19+
20+
test_that("Vector of strings match", {
21+
expect_identical(fizz_buzz(10), ans10)
22+
expect_identical(fizz_buzz(100), ans100)
23+
})
24+
25+
message("All tests passed for exercise: fizz-buzz")

0 commit comments

Comments
 (0)