-
-
Notifications
You must be signed in to change notification settings - Fork 41
Added famous fizz-buzz question #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } | ||
|
|
||
katrinleinweber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| out <- sapply(seq(1, input), replace_number) | ||
| return(out) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| fizz_buzz <- function(input) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.