Avatar of shmibs

shmibs's solution

to Raindrops in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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
  Code.load_file("raindrops.exs", __DIR__)
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

  @fac2str [{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(integer) :: String.t
  def convert(number) do
    Enum.reduce(@fac2str, {number, ""}, &check_rem/2)
    |> (fn {i,s} -> if s == "", do: Integer.to_string(i), else: s end).()
  end

  defp check_rem({k, v}, {n, s}) do
    cond do
      rem(n, k) == 0 -> {n, s <> v }
      true -> {n, s}
    end
  end

end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?