🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of meatbot

meatbot's solution

to Raindrops in the Erlang Track

Published at Apr 07 2021 · 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

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit

Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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_tests.erl

%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/raindrops/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(raindrops_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").




'1_the_sound_for_1_is_1_test_'() ->
    {"the sound for 1 is 1",
     ?_assertEqual("1", raindrops:convert(1))}.

'2_the_sound_for_3_is_pling_test_'() ->
    {"the sound for 3 is Pling",
     ?_assertEqual("Pling", raindrops:convert(3))}.

'3_the_sound_for_5_is_plang_test_'() ->
    {"the sound for 5 is Plang",
     ?_assertEqual("Plang", raindrops:convert(5))}.

'4_the_sound_for_7_is_plong_test_'() ->
    {"the sound for 7 is Plong",
     ?_assertEqual("Plong", raindrops:convert(7))}.

'5_the_sound_for_6_is_pling_as_it_has_a_factor_3_test_'() ->
    {"the sound for 6 is Pling as it has a "
     "factor 3",
     ?_assertEqual("Pling", raindrops:convert(6))}.

'6_2_to_the_power_3_does_not_make_a_raindrop_sound_as_3_is_the_exponent_not_the_base_test_'() ->
    {"2 to the power 3 does not make a raindrop "
     "sound as 3 is the exponent not the base",
     ?_assertEqual("8", raindrops:convert(8))}.

'7_the_sound_for_9_is_pling_as_it_has_a_factor_3_test_'() ->
    {"the sound for 9 is Pling as it has a "
     "factor 3",
     ?_assertEqual("Pling", raindrops:convert(9))}.

'8_the_sound_for_10_is_plang_as_it_has_a_factor_5_test_'() ->
    {"the sound for 10 is Plang as it has "
     "a factor 5",
     ?_assertEqual("Plang", raindrops:convert(10))}.

'9_the_sound_for_14_is_plong_as_it_has_a_factor_of_7_test_'() ->
    {"the sound for 14 is Plong as it has "
     "a factor of 7",
     ?_assertEqual("Plong", raindrops:convert(14))}.

'10_the_sound_for_15_is_pling_plang_as_it_has_factors_3_and_5_test_'() ->
    {"the sound for 15 is PlingPlang as it "
     "has factors 3 and 5",
     ?_assertEqual("PlingPlang", raindrops:convert(15))}.

'11_the_sound_for_21_is_pling_plong_as_it_has_factors_3_and_7_test_'() ->
    {"the sound for 21 is PlingPlong as it "
     "has factors 3 and 7",
     ?_assertEqual("PlingPlong", raindrops:convert(21))}.

'12_the_sound_for_25_is_plang_as_it_has_a_factor_5_test_'() ->
    {"the sound for 25 is Plang as it has "
     "a factor 5",
     ?_assertEqual("Plang", raindrops:convert(25))}.

'13_the_sound_for_27_is_pling_as_it_has_a_factor_3_test_'() ->
    {"the sound for 27 is Pling as it has "
     "a factor 3",
     ?_assertEqual("Pling", raindrops:convert(27))}.

'14_the_sound_for_35_is_plang_plong_as_it_has_factors_5_and_7_test_'() ->
    {"the sound for 35 is PlangPlong as it "
     "has factors 5 and 7",
     ?_assertEqual("PlangPlong", raindrops:convert(35))}.

'15_the_sound_for_49_is_plong_as_it_has_a_factor_7_test_'() ->
    {"the sound for 49 is Plong as it has "
     "a factor 7",
     ?_assertEqual("Plong", raindrops:convert(49))}.

'16_the_sound_for_52_is_52_test_'() ->
    {"the sound for 52 is 52",
     ?_assertEqual("52", raindrops:convert(52))}.

'17_the_sound_for_105_is_pling_plang_plong_as_it_has_factors_3_5_and_7_test_'() ->
    {"the sound for 105 is PlingPlangPlong "
     "as it has factors 3, 5 and 7",
     ?_assertEqual("PlingPlangPlong",
		   raindrops:convert(105))}.

'18_the_sound_for_3125_is_plang_as_it_has_a_factor_5_test_'() ->
    {"the sound for 3125 is Plang as it has "
     "a factor 5",
     ?_assertEqual("Plang", raindrops:convert(3125))}.
-module(raindrops).

-export([convert/1]).


convert(Number) ->
  List = lists:foldr(
    fun({Factor, String}, Acc) ->
      case Number rem Factor == 0 of
        true -> [String | Acc];
        false -> Acc
      end
    end,
    [],
    [{3, "Pling"}, {5, "Plang"}, {7, "Plong"}]
  ),

  case List of
    [] -> integer_to_list(Number);
    _ -> lists:flatten(List)
  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?