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

# thekeele's solution

## to Raindrops in the Elixir Track

Published at May 17 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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

## Running tests

Execute the tests with:

``````\$ elixir raindrops_test.exs
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule RaindropsTest do
use ExUnit.Case

# @tag :pending
test "1" do
assert Raindrops.convert(1) == "1"
end

@tag :pending
test "3" do
assert Raindrops.convert(3) == "Pling"
end

@tag :pending
test "5" do
assert Raindrops.convert(5) == "Plang"
end

@tag :pending
test "7" do
assert Raindrops.convert(7) == "Plong"
end

@tag :pending
test "6" do
assert Raindrops.convert(6) == "Pling"
end

@tag :pending
test "9" do
assert Raindrops.convert(9) == "Pling"
end

@tag :pending
test "10" do
assert Raindrops.convert(10) == "Plang"
end

@tag :pending
test "14" do
assert Raindrops.convert(14) == "Plong"
end

@tag :pending
test "15" do
assert Raindrops.convert(15) == "PlingPlang"
end

@tag :pending
test "21" do
assert Raindrops.convert(21) == "PlingPlong"
end

@tag :pending
test "25" do
assert Raindrops.convert(25) == "Plang"
end

@tag :pending
test "35" do
assert Raindrops.convert(35) == "PlangPlong"
end

@tag :pending
test "49" do
assert Raindrops.convert(49) == "Plong"
end

@tag :pending
test "52" do
assert Raindrops.convert(52) == "52"
end

@tag :pending
test "105" do
assert Raindrops.convert(105) == "PlingPlangPlong"
end

@tag :pending
test "12121" do
assert Raindrops.convert(12121) == "12121"
end
end``````
``````defmodule Raindrops do

@raindrops %{3 => "Pling", 5 => "Plang", 7 => "Plong"}

@spec convert(pos_integer) :: String.t()
def convert(number) do
case modulo(number, Map.keys(@raindrops)) do
"" -> "#{number}"
raindrops_sound -> raindrops_sound
end
end

defp modulo(_, []),
do: ""
defp modulo(number, [divisor | rest]) when rem(number, divisor) == 0,
do: @raindrops[divisor] <> modulo(number, rest)
defp modulo(number, [_ | rest]),
do: "" <> modulo(number, rest)
end``````