Skip to content
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

R raindrops #350

Merged
merged 2 commits into from
Apr 9, 2024
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
1 change: 1 addition & 0 deletions r/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

## [Exercises](https://exercism.org/tracks/r/exercises)

- [raindrops](./raindrops/README.md)
24 changes: 24 additions & 0 deletions r/raindrops/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"authors": [
"jonmcalder"
],
"contributors": [
"jonboiser",
"katrinleinweber",
"zacchaeusluke"
],
"files": {
"solution": [
"raindrops.R"
],
"test": [
"test_raindrops.R"
],
"example": [
".meta/example.R"
]
},
"blurb": "Convert a number into its corresponding raindrop sounds - Pling, Plang and Plong.",
"source": "A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division.",
"source_url": "https://en.wikipedia.org/wiki/Fizz_buzz"
}
1 change: 1 addition & 0 deletions r/raindrops/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"r","exercise":"raindrops","id":"475418e2df784a60bdcccccbb4f71b2e","url":"https://exercism.org/tracks/r/exercises/raindrops","handle":"vpayno","is_requester":true,"auto_approve":false}
38 changes: 38 additions & 0 deletions r/raindrops/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Help

## Running the tests

Tests require the `{testthat}` package to be installed in R.
To run the tests for an exercise, simply execute the `test_<exercise_name>.R` script from within the exercise's directory.

This can be conveniently done with [testthat's `auto_test` function](https://testthat.r-lib.org/reference/auto_test.html). 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`.

See the [tests page](https://exercism.org/docs/tracks/r/tests) for more information.

## Submitting your solution

You can submit your solution using the `exercism submit raindrops.R` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [R track's documentation](https://exercism.org/docs/tracks/r)
- The [R track's programming category on the forum](https://forum.exercism.org/c/programming/r)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can try one of the following resources:

- [StackOverflow](https://stackoverflow.com/questions/tagged/r) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
- [#rstats](https://twitter.com/search?q=%23rstats) is the hashtag to use if you are asking for help or guidance on Twitter. The R community is very active on Twitter and always try to help those who are new to R.
- [/r/rstats](https://www.reddit.com/r/rstats) is the R subreddit.
- [RStudio Community](https://community.rstudio.com/) is another active and helpful R community.
54 changes: 54 additions & 0 deletions r/raindrops/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Raindrops

Welcome to Raindrops on Exercism's R Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Introduction

Raindrops is a slightly more complex version of the FizzBuzz challenge, a classic interview question.

## Instructions

Your task is to convert a number into its corresponding raindrop sounds.

If a given number:

- is divisible by 3, add "Pling" to the result.
- is divisible by 5, add "Plang" to the result.
- is divisible by 7, add "Plong" to the result.
- **is not** divisible by 3, 5, or 7, the result should be the number as a string.

## Examples

- 28 is divisible by 7, but not 3 or 5, so the result would be `"Plong"`.
- 30 is divisible by 3 and 5, but not 7, so the result would be `"PlingPlang"`.
- 34 is not divisible by 3, 5, or 7, so the result would be `"34"`.

~~~~exercism/note
A common way to test if one number is evenly divisible by another is to compare the [remainder][remainder] or [modulus][modulo] to zero.
Most languages provide operators or functions for one (or both) of these.

[remainder]: https://exercism.org/docs/programming/operators/remainder
[modulo]: https://en.wikipedia.org/wiki/Modulo_operation
~~~~

## Source

### Created by

- @jonmcalder

### Contributed to by

- @jonboiser
- @katrinleinweber
- @zacchaeusluke

### Based on

A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz

### My Solution

- [raindrops.R](./raindrops.R)
- [run-tests](./run-tests-r.txt)
24 changes: 24 additions & 0 deletions r/raindrops/raindrops.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
raindrops <- function(number) {
sounds <- ""

# using 3 different ways of appending to a string

if (number %% 3 == 0) {
sounds <- paste(sounds, "Pling", sep = "")
}

if (number %% 5 == 0) {
sounds <- paste0(sounds, "Plang")
}

if (number %% 7 == 0) {
sounds <- append(sounds, "Plong")
sounds <- paste(sounds, collapse = "")
}

if (sounds == "") {
sounds <- as.character(number)
}

return(sounds)
}
79 changes: 79 additions & 0 deletions r/raindrops/run-tests-r.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Running automated test file(s):


===============================================================================

Running: ../../.github/citools/r/r-test

Running R Tests

R versions:

R version 4.3.3 (2024-02-29) -- "Angel Food Cake"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.


Rscript (R) version 4.3.3 (2024-02-29)


==============================================================================

Running: Rscript test_raindrops.R

Test passed 🎉
Test passed 🌈
Test passed 🌈
Test passed 🎉
Test passed 😸
Test passed 😸
Test passed 🥇
Test passed 🥇
Test passed 🎉
Test passed 🎉
Test passed 😀
Test passed 🥇
Test passed 😀
Test passed 😸
Test passed 🎉
Test passed 🥇
Test passed 🎉
Test passed 🌈

real 0m1.009s
user 0m0.950s
sys 0m0.060s


==============================================================================

Exit code: 0

real 0m1.127s
user 0m1.013s
sys 0m0.125s

real 0m1.129s
user 0m1.015s
sys 0m0.126s

===============================================================================

Running: misspell ./raindrops.R ./test_raindrops.R

real 0m0.022s
user 0m0.018s
sys 0m0.012s

===============================================================================

/home/vpayno/git_vpayno/exercism-workspace/r

===============================================================================

93 changes: 93 additions & 0 deletions r/raindrops/test_raindrops.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
source("./raindrops.R")
library(testthat)

test_that("the sound for 1 is 1", {
number <- 1
expect_equal(raindrops(number), "1")
})

test_that("the sound for 3 is Pling", {
number <- 3
expect_equal(raindrops(number), "Pling")
})

test_that("the sound for 5 is Plang", {
number <- 5
expect_equal(raindrops(number), "Plang")
})

test_that("the sound for 7 is Plong", {
number <- 7
expect_equal(raindrops(number), "Plong")
})

test_that("the sound for 6 is Pling as it has a factor 3", {
number <- 6
expect_equal(raindrops(number), "Pling")
})

test_that("2 to the power 3 does not make a raindrop sound as 3 is the exponent
not the base", {
number <- 8
expect_equal(raindrops(number), "8")
})

test_that("the sound for 9 is Pling as it has a factor 3", {
number <- 9
expect_equal(raindrops(number), "Pling")
})

test_that("the sound for 10 is Plang as it has a factor 5", {
number <- 10
expect_equal(raindrops(number), "Plang")
})

test_that("the sound for 14 is Plong as it has a factor of 7", {
number <- 14
expect_equal(raindrops(number), "Plong")
})

test_that("the sound for 15 is PlingPlang as it has factors 3 and 5", {
number <- 15
expect_equal(raindrops(number), "PlingPlang")
})

test_that("the sound for 21 is PlingPlong as it has factors 3 and 7", {
number <- 21
expect_equal(raindrops(number), "PlingPlong")
})

test_that("the sound for 25 is Plang as it has a factor 5", {
number <- 25
expect_equal(raindrops(number), "Plang")
})

test_that("the sound for 27 is Pling as it has a factor 3", {
number <- 27
expect_equal(raindrops(number), "Pling")
})

test_that("the sound for 35 is PlangPlong as it has factors 5 and 7", {
number <- 35
expect_equal(raindrops(number), "PlangPlong")
})

test_that("the sound for 49 is Plong as it has a factor 7", {
number <- 49
expect_equal(raindrops(number), "Plong")
})

test_that("the sound for 52 is 52", {
number <- 52
expect_equal(raindrops(number), "52")
})

test_that("the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7", {
number <- 105
expect_equal(raindrops(number), "PlingPlangPlong")
})

test_that("the sound for 3125 is Plang as it has a factor 5", {
number <- 3125
expect_equal(raindrops(number), "Plang")
})
Loading