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

angelikatyborska's solution

to Raindrops in the Elixir Track

Published at Feb 01 2020 · 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".

Running tests

Execute the tests with:

``````\$ mix test
``````

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

``````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``````

test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule Raindrops do
@factors [
{3, "Pling"},
{5, "Plang"},
{7, "Plong"}
]
@doc """
Returns a string based on raindrop factors.

- If the number contains 3 as a prime factor, output 'Pling'.
- If the number contains 5 as a prime factor, output 'Plang'.
- If the number contains 7 as a prime factor, output 'Plong'.
- If the number does not contain 3, 5, or 7 as a prime factor,
just pass the number's digits straight through.
"""
@spec convert(pos_integer) :: String.t()
def convert(number) do
output =
Enum.reduce(@factors, "", fn {factor, sound}, acc ->
if rem(number, factor) == 0 do
acc <> sound
else
acc
end
end)

if output == "" do
to_string(number)
else
output
end
end
end``````