raindrops
This commit is contained in:
30
elixir/raindrops/lib/raindrops.ex
Normal file
30
elixir/raindrops/lib/raindrops.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule Raindrops do
|
||||
@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.
|
||||
"""
|
||||
@results [
|
||||
{3, "Pling"},
|
||||
{5, "Plang"},
|
||||
{7, "Plong"}
|
||||
]
|
||||
|
||||
@spec convert(pos_integer) :: String.t()
|
||||
def convert(number) do
|
||||
@results
|
||||
|> Enum.map_join(&mapper(number, &1))
|
||||
|> case do
|
||||
"" -> Integer.to_string(number)
|
||||
result -> result
|
||||
end
|
||||
end
|
||||
|
||||
defp mapper(number, {factor, value}) do
|
||||
if rem(number, factor) == 0, do: value, else: ""
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user