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

# bkhl's solution

## to Raindrops in the D Track

Published at Aug 23 2019 · 0 comments
Instructions
Test suite
Solution

Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the modulo operation.

The rules of `raindrops` are that if a given number:

• has 3 as a factor, add 'Pling' to the result.
• has 5 as a factor, add 'Plang' to the result.
• has 7 as a factor, add 'Plong' to the result.
• does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.

## Examples

• 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
• 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
• 34 is not factored by 3, 5, or 7, so the result 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 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

## Submitting Incomplete Solutions

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

### raindrops.d

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

// The sound for 1 is 1
assert(convert(1) == "1");

static if (allTestsEnabled)
{
// The sound for 3 is Pling
assert(convert(3) == "Pling");

// The sound for 5 is Plang
assert(convert(5) == "Plang");

// the sound for 7 is Plong
assert(convert(7) == "Plong");

// The sound for 6 is Pling as it has a factor 3
assert(convert(6) == "Pling");

// 2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base
assert(convert(8) == "8");

// The sound for 9 is Pling as it has a factor 3
assert(convert(9) == "Pling");

// The sound for 10 is Plang as it has a factor 5
assert(convert(10) == "Plang");

// The sound for 14 is Plong as it has a factor of 7
assert(convert(14) == "Plong");

// The sound for 15 is PlingPlang as it has factors 3 and 5
assert(convert(15) == "PlingPlang");

// The sound for 21 is PlingPlong as it has factors 3 and 7
assert(convert(21) == "PlingPlong");

// The sound for 25 is Plang as it has a factor 5
assert(convert(25) == "Plang");

// The sound for 27 is Pling as it has a factor 3
assert(convert(27) == "Pling");

// The sound for 35 is PlangPlong as it has factors 5 and 7
assert(convert(35) == "PlangPlong");

// The sound for 49 is Plong as it has a factor 7
assert(convert(49) == "Plong");

// The sound for 52 is 52
assert(convert(52) == "52");

// The sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7
assert(convert(105) == "PlingPlangPlong");

// The sound for 3125 is Plang as it has a factor 5
assert(convert(3125) == "Plang");
}

}``````
``````module raindrops;

import std.algorithm.iteration;
import std.string;
import std.range;
import std.typecons;

alias Mapping = Tuple!(int, "factor", string, "sound");
auto mappings = [Mapping(3, "Pling"), Mapping(5, "Plang"), Mapping(7, "Plong")];

string convert(int input)
{
auto result = mappings.map!(m => input % m.factor == 0 ? m.sound : "").join;
return result.empty ? format!"%d"(input) : result;
}

unittest
{
assert(convert(1) == "1");
assert(convert(3) == "Pling");
assert(convert(5) == "Plang");
assert(convert(7) == "Plong");
assert(convert(6) == "Pling");
assert(convert(8) == "8");
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(27) == "Pling");
assert(convert(35) == "PlangPlong");
assert(convert(49) == "Plong");
assert(convert(52) == "52");
assert(convert(105) == "PlingPlangPlong");
assert(convert(3125) == "Plang");
}``````