# kgori's solution

## to Raindrops in the D Track

Published at Nov 04 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".

## Getting Started

Make sure you have read D page on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

``````static if (all_tests_enabled)
``````

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard D facilities in preference to writing your own low-level algorithms or facilities by hand. DRefLanguage and DReference are references to the D language and D standard library.

## 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.

### raindrops.d

``````unittest {
const int allTestsEnabled = 0;

assert(convert(1) == "1");
static if (allTestsEnabled) {
assert(convert(3) == "Pling");
assert(convert(5) == "Plang");
assert(convert(7) == "Plong");
assert(convert(6) == "Pling");
assert(convert(9) == "Pling");
assert(convert(10) == "Plang");
assert(convert(14) == "Plong");
assert(convert(15) == "PlingPlang");
assert(convert(21) == "PlingPlong");
assert(convert(25) == "Plang");
assert(convert(35) == "PlangPlong");
assert(convert(49) == "Plong");
assert(convert(52) == "52");
assert(convert(105) == "PlingPlangPlong");
}

}``````
``````module raindrops;
import std.array : appender;
import std.conv : to;

string convert(int num) {
auto output = appender!string;
if (num % 3 == 0) output ~= "Pling";
if (num % 5 == 0) output ~= "Plang";
if (num % 7 == 0) output ~= "Plong";
return output.data ? output.data : to!string(num);
}

unittest {
const int allTestsEnabled = 1;

assert(convert(1) == "1");
static if (allTestsEnabled) {
assert(convert(3) == "Pling");
assert(convert(5) == "Plang");
assert(convert(7) == "Plong");
assert(convert(6) == "Pling");
assert(convert(9) == "Pling");
assert(convert(10) == "Plang");
assert(convert(14) == "Plong");
assert(convert(15) == "PlingPlang");
assert(convert(21) == "PlingPlong");
assert(convert(25) == "Plang");
assert(convert(35) == "PlangPlong");
assert(convert(49) == "Plong");
assert(convert(52) == "52");
assert(convert(105) == "PlingPlangPlong");
}

}``````