ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# katrinleinweber's solution

## to Raindrops in the R Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Convert a number to a string, the contents of which depend on the number's factors.

• If the number has 3 as a factor, output 'Pling'.
• If the number has 5 as a factor, output 'Plang'.
• If the number has 7 as a factor, output 'Plong'.
• If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

## Examples

• 28's factors are 1, 2, 4, 7, 14, 28.
• In raindrop-speak, this would be a simple "Plong".
• 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
• In raindrop-speak, this would be a "PlingPlang".
• 34 has four factors: 1, 2, 17, and 34.
• In raindrop-speak, this would be "34".

## Installation

See this guide 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. 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

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_raindrops.R

``````source("./raindrops.R")
library(testthat)

context("raindrops")

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")
})

message("All tests passed for exercise: raindrops")``````
``````raindrops <- function(number) {

sound <- ""

if (number %% 3 == 0)
sound <- append(sound, 'Pling')

if (number %% 5 == 0)
sound <- append(sound, 'Plang')

if (number %% 7 == 0)
sound <- append(sound, 'Plong')

if (!(number %% 3 == 0 | number %% 5 == 0 | number %% 7 == 0))
return(as.character(number))

paste(sound, collapse = "")
}``````